Skip to content

Instantly share code, notes, and snippets.

@MouseZero
Last active September 25, 2017 02:17
Show Gist options
  • Save MouseZero/e3181d5d2780e56d7f5a531e3cce84f9 to your computer and use it in GitHub Desktop.
Save MouseZero/e3181d5d2780e56d7f5a531e3cce84f9 to your computer and use it in GitHub Desktop.
Pipe Promises
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