1

I am a beginner in Angular2 programming. I am trying to write a function that returns a custom promise. It looks like this:

removeItem(item, removeFile: boolean = true): Promise <any> {
        // remove the item from the list
        let index = this.items.indexOf(item);
        if (index > -1) { this.items.splice(index, 1); }
       // now if removeFile is true, also physical remove the image
       if (removeFile) {
         // file.removeFile is basically cordova's file removeFile that returns a promise
         return file.removeFile(file.dataDirectory, item.photo[i]);
       } 
       else { // return my own promise so its consistent 
         return Promise.resolve(); //<---- *THIS*
       }

}

The question I have is the line commented as *THIS* at the bottom. Every example I have seen does a var p = new Promise(resolve, reject) (one example of many).

Questions:

1) Why do I need to do that? The method above seems to work. I read this SO thread that answers it for another promise framework, but I can't wrap my head around it. Can anyone parse it for me?

2) If I must do a p = new Promise, I don't want to wrap my entire functionality inside its function like the examples show. Can I just do a return p.resolve() and p.reject() where I need to so I don't add to nesting?

3) To dive a bit more into what @Maximus said:

You need a new Promise whenever there's some async work you wish to perform and then either resolve or reject the promise

Why can't I use the same approach as above (that is, use Promise.resolve/reject)? For example, lets say I force a custom promise on file.removeFile (I know this is bad, but I'm just trying to understand why I need a new promise for async work)

if (removeFile) {
         file.removeFile(file.dataDirectory, item.photo[i])
         .then ({success=>return (Promise.resolve(success);},
                {error=>return (Promise.reject(error);});
       } 

Edit: To clarify, I know perfectly well I don't need to wrap removeFile with my own promise. This was just to give an example of wrapping an async job with my own promise and yet not using new Promise. In other words, can I learn from an example of an async job that would require me to do a new Promise and where Promise.resolve would cause issues?

thanks.

user1361529
  • 2,667
  • 29
  • 61

1 Answers1

1

Why do I need to do that? The method above seems to work.

You don't need it. You variant:

return Promise.resolve();

is absolutely fine. The code you used:

Promise.resolve(value)

is sort of an alias for

new Promise((resolve)=>{resolve(value)})

You need a new Promise whenever there's some async work you wish to perform and then either resolve or reject the promise. For example, resolve a promise in 1 second using setTimeout:

new Promise((resolve)=>{setTimeout(()=>{resolve(value)}, 1000)})

Edit:

You don't need to use Promise.resolve() here:

 file.removeFile(file.dataDirectory, item.photo[i])
 .then ({success=>return (Promise.resolve(success);},
        {error=>return (Promise.reject(error);});

This is the same:

 file.removeFile(file.dataDirectory, item.photo[i])
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • Thanks Maximus. I edited my original question to add a further question on why I need a a new promise even for async work – user1361529 Jul 11 '17 at 19:31
  • @user1361529, you probably have to learn how promises work. I added details regarding your update to my answer. – Max Koretskyi Jul 11 '17 at 19:39
  • @Maximus Would be great if you provided some link to understand how promises work – yurzui Jul 11 '17 at 19:41
  • @Maximus - I think you misunderstood my edit. I know I don't need it and I can return the promise returned by file.removeFile. That is what I am doing in my original code. The reason I edited it was to question why a `new Promise` is required for async work as you stated. – user1361529 Jul 11 '17 at 19:43
  • how would you resolve a promise in 1 second without `new Promise()`? – Max Koretskyi Jul 11 '17 at 19:49
  • `setTimeout(()=>{return Promise.resolve();},1000);` won't work? Is the problem here that it the parent function will return `undefined` till this triggers in 1 second? – user1361529 Jul 11 '17 at 19:57
  • Okay, I don't think I really get a complete gist of where `new Promise` is really needed (I don't fully understand your last comment), but I think that is a different question on its own. Given you answered my key question - that is, my first method was fine for what I am intending to do, I'll accept that answer. I might post a separate question detailing only item 3) later. Thanks. – user1361529 Jul 11 '17 at 20:02
  • try building a chain of promises each resolving within a second and you'll see why `new Promise` is needed – Max Koretskyi Jul 11 '17 at 20:03