JavaScript - Promises - Points To Remember

javascript-promises

// JavaScript - Promises - Points to remember:

If the promise is resolved, the control will go to the then callbacks.

If the promise is rejected, the control will move forward to the next then() 
that has a failure callback, or to the next catch().  Apart from explict promise 
rejection, the catch callback is also called when any exception is thrown.

Throwing an exception in a Promise automatically reject that promise.

The then callbacks and the catch callbacks are executed in the the order 
that they were registered.  The second then callback is not invoked until the 
first callback finishes.

If an error is thrown, it automatically invoke the callback methods that are 
specified using the the .catch method.

Sub-sequent "then" callbacks are not invoked if an error occurred within the 
first "then" callback.

If a promise has already been fulfilled (either resolved or rejected), and we 
later attach callbacks to it, the callbacks will be immediately invoked.

Each then callback receive the result of the previous then callback return value.

If a promise has already been resolved but then is called again, the then 
callback is immediately invoked.  If the promise was rejected, and we call the 
then function, the then callback is never invoked, but the failure function is 
invoked if we specified the failure function.

If we register multiple then callbacks, only the first then callback receives 
the resolved value as parameter.  Sub-sequent .then callbacks do not 
receive this data.  Instead, they receive the result of the previous .then 
callbacks.

We may often incorrectly assume that  these then callbacks always perform additional 
asynchronous operations and therefore cannot return value.  This is not necessarily true.  
These then callbacks are functions, and they can always return value.  If we need to do 
asynchronous operation inside these then callbacks, we should return a promise 
that does the asynchronous operation.  

If the first then callback returns a promise, the second then callback is not 
invoked until that promise is resolved.

When we return a simple value inside then(), the next then() is called with that 
return value.  But if we return a promise inside then(), the next then() waits 
on it and gets called when that promise is settled.  Is this really the case?  
Does the second then callback really receive the data from the second promise or 
does it receive the second promise as a parameter?

We cannot have an empty promise.  We cannot externally resolve a promise, 
using Promise.resolve(aPromise).  Perhaps we can create a promise using 
new Promise() without specifying executor function, or we can create a promise 
using Promise.resolve(), but in the case of using Promise.resolve(), the promise 
is already resolved.  I do not know how to externally resolve a promise yet.  
Perhaps, in such cases, we have to use Deferred?

Alternatively, perhaps, to resolve a promise externally, we can use the bind function:
var p = new Promise(function(resolve, reject){
    this.onclick = resolve;
}.bind(this));
See:
http://bit.ly/2r7PNqH
http://lea.verou.me/2016/12/resolve-promises-externally-with-this-one-weird-trick/
http://bit.ly/2r7PNqH

The .then method always returns a promise, which may be the same promise that it 
was invoked with (the this object).  However, if the then callback returns a promise, 
the then method will return that promise instead.  This allows us to chain multiple 
then callbacks:

return asyncFunction()
  .then(function(value) {...})  // first callback
  .then(function(value) {...}); // second callback

We can have a function that creates a promise, attach some callbacks to the promise, 
and returns the promise.  The caller of our function can attach more callbacks to 
the promise if desired.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License