Created
June 15, 2018 07:49
-
-
Save yowcow/119b4b0eecbae43179ccfb6f366b37f1 to your computer and use it in GitHub Desktop.
A chain of Promises
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 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