Skip to content

Instantly share code, notes, and snippets.

@yowcow
Created June 15, 2018 07:49
Show Gist options
  • Save yowcow/119b4b0eecbae43179ccfb6f366b37f1 to your computer and use it in GitHub Desktop.
Save yowcow/119b4b0eecbae43179ccfb6f366b37f1 to your computer and use it in GitHub Desktop.
A chain of Promises
const waits = [1, 2, 1, 2, 0, 1, 2, 1, 2];
const p = waits.reduce(
function (p, val) {
return p.then(function () {
return new Promise(function (resolve, reject) {
setTimeout(
function () {
if (val === 0) {
console.log(`${val} 秒は待てません`)
reject();
} else {
console.log(`${val} 秒待ちました`)
resolve();
}
},
val * 1000
)
});
});
},
Promise.resolve()
).then(function () {
console.log("すべて終わりました");
}).catch(function () {
console.log("何か変でした");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment