Created
March 4, 2019 16:57
-
-
Save srebalaji/b4699e16767f5907d7216d26d71abf41 to your computer and use it in GitHub Desktop.
A simple JS file that explains Promise.all
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // A simple promise that resolves after a given time | |
| const timeOut = (t) => { | |
| return new Promise((resolve, reject) => { | |
| setTimeout(() => { | |
| resolve(`Completed in ${t}`) | |
| }, t) | |
| }) | |
| } | |
| // Resolving a normal promise. | |
| timeOut(1000) | |
| .then(result => console.log(result)) // Completed in 1000 | |
| // Promise.all | |
| Promise.all([timeOut(1000), timeOut(2000)]) | |
| .then(result => console.log(result)) // ["Completed in 1000", "Completed in 2000"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment