these chainz of promise
come to me, cover me, hold me together we'll break these chains of promise don't give up, don't give up together with me and my baby break the chains of promise
by edwardsharp
CSS
body{
color: white;
background: black;
}
JavaScript
log = (msg) => {
document.body.innerHTML += `<pre>${msg}</pre>`
return msg
}
aPromise = (timeout, fail) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (fail) {
reject()
} else {
resolve()
}
}, timeout)
})
}
fetchStuff = () => {
return new Promise((resolve, reject) => {
Promise.all([
aPromise(1, false).then(() => log(1)),
aPromise(50, false).then(() => log(2)),
false,
aPromise(100, true).then(() => log(3)).catch(() => log('WARN! 3 caught')),
aPromise(150, false).then(() => log(4)),
]).then((stuff) => resolve(stuff))
})
}
fetchStuff().then((stuff) => log('fetchd stuff: '+stuff)).catch(() => log('WARN! fetchStuff caught!'))