Showing posts with label nodej. Show all posts
Showing posts with label nodej. Show all posts

Thursday, October 5, 2017

Javascript Promise chaining

Hi there here is the third post of Javascript/NodeJs callback.

We have an async task with Promise:

[gist https://gist.github.com/vanduc1102/d7c84cd605edf8d0faf1fb620556ba5f]

Create tasks function:

[gist https://gist.github.com/vanduc1102/d07c13973a4c3cda3973b12f6619f1b9]

Promise.all


execute all the promises at the same times, and resolve when all of the promises resolved

[gist https://gist.github.com/vanduc1102/8b85ee8ce42d23fb49ae9d165bb19794]

and reject when any of the Promise was rejected:

[gist https://gist.github.com/vanduc1102/09a90b3923ca1ba3d5d72630958e950a]

Promise.race


The Promise.race(iterable) method returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.

[gist https://gist.github.com/vanduc1102/21ff8e6900ebfb1824c965fb1d654291]

Sequence Promises


Sometimes we need to do the jobs in sequences.
[gist https://gist.github.com/vanduc1102/5264a38f20f54d04360a191e53c5e7e3]

For promise functions with multiple parameters

[gist https://gist.github.com/vanduc1102/b15f3f2ff5733ebbc467464e96cdf2ea]

 

Online editor

https://repl.it/@vanduc1102/sequence-promises