Skip to content

Instantly share code, notes, and snippets.

@leny
Created February 15, 2019 08:23
Show Gist options
  • Save leny/6c792605a5087cec84705ba62c118b43 to your computer and use it in GitHub Desktop.
Save leny/6c792605a5087cec84705ba62c118b43 to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/xojajom
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