Created
February 15, 2019 08:23
-
-
Save leny/6c792605a5087cec84705ba62c118b43 to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/xojajom
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
const randomWait = (delay) => new Promise((resolve, reject) => { | |
setTimeout(() => { | |
if (Math.round(Math.random() * 2) % 2) { | |
resolve("ok"); | |
} else { | |
reject(new Error("wrong!")); | |
} | |
}, delay * 1000); | |
}); | |
randomWait(5) | |
.then((result) => {console.log("Once:",result);}) | |
.catch((error) => {console.log("Once:",error.message);}); | |
Promise | |
.all([randomWait(2), randomWait(1), randomWait(3)]) | |
.then((result) => {console.log("Promise.all:",result);}) | |
.catch((error) => {console.log("Promise.all:",error.message);}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment