This tutorial is splitted in sections. Each one devoted to a particular argument.
All the section that have 'buttons' have also an underline javascript code accessible keeping the browser in debug mode: F12, will help to locate the topic points..
Working with the 'Promise' object involves these concepts:
Once received a message ('resolve' xOr 'reject'), the 'Promise' is ready to call the methods listed at the introduction point 3). 'PromiseGUI' is the tool used by this page to give a graphical representation of the action taken by the 'Promise'.
In the real world:
To properly control a 'Promise' it's important to know how it reacts to an exception. 'Promise' is a docile tool, the only real attention is needed during the instantiation: new Promise(function (resolve, reject) {...});
The next sections will analyze the effect of an exception occurring in different places.
Regarding the function 'finally', an exception in it will redirect the flow to the function 'catch'. An exception can occur also in the 'catch' function (why not?). To cope with this event, you can choose between two strategies: * A 'try-catch' session. * Chaining the exception to another 'catch' handler, but this... is another story :)
Now it's time to put things together...(When the going gets tough, the tough get going.)
Nothing so special has happened so far. What we have done with 'Promise' can be easily implemented by any developer. Now it's time to show 'Promise' in all its glory.
In the following sections, there will be a 'Master-Promise' and many 'Promises'. The 'MasterPromise' will fail or succeed, based its own configuration and what happens to its promises.
Now some, more in-depth, consideration regarding: 1) concurrency and threading Starting from section 4A, a single 'Master-Promise' controls many, independent and simultaneous processes. This is not a problem under javascript. In fact, although a 'Promise' is controlling any number of processes, the javascript engine works on a single thread, so there is only one process running in a given time. Hence, when a 'start' function gets its 'CPU Time slot', it has the time to set its state and then notifying 'Promise' without worrying if another process might leave it in an inappropriate condition.
How this affects 'Promise'?
Configuring a 'Master-Promise' is a 3 steps process. // 1) creating the array of promises where each element is a 'Promise', controlling its own process: var promises = [ new Promise(process1), new Promise(process2), ... , new Promise(processN)]; // 2) creating the masterPromise: var masterPromise = Promise.all(promises); // 3) Let the masterPromise doing its job. masterPromise .then( ... ) .finally( ... ) .catch( ... ); the fact is: they must be consecutive, but not necessarily in the same function! The constraint is, masterPromise must be created, at least, immediately before the first resolve/reject is called.
that's all folks. Hoping you enjoyed this page. Bye :)