Last active
September 25, 2017 02:17
-
-
Save MouseZero/e3181d5d2780e56d7f5a531e3cce84f9 to your computer and use it in GitHub Desktop.
Pipe Promises
This file contains 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
function promisePipe(promises, argsToFirstPromise) { | |
return promises.reduce((prev, promise) => { | |
return prev.then(x => promise(x)) | |
}, Promise.resolve(argsToFirstPromise)) | |
} | |
// use promise pipe | |
promisePipe([ | |
add1, | |
add1, | |
add1, | |
printPromise, | |
add12, | |
add1, | |
printPromise | |
], 1) | |
.catch(x => console.log(x)) | |
// use promise pipe with a reject (need to uncomment to test) | |
// promisePipe([ | |
// add1, | |
// reject, | |
// add12, | |
// add1, | |
// printPromise | |
// ], 1) | |
// .catch(x => console.log(x)) | |
// Bunch of diffrent promise types | |
function add1 (x) { | |
return new Promise((resolve, reject) => { | |
resolve (x + 1) | |
}) | |
} | |
async function add12(x) { | |
console.log('from async function') | |
return x + 12 | |
} | |
function printPromise (x) { | |
console.log(x) | |
return Promise.resolve(x) | |
} | |
function reject (x) { | |
return Promise.reject('this is what failure looks like') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment