Last active
January 24, 2018 08:22
-
-
Save fnky/33e9ad34504da9a9e852c5cbf2e80830 to your computer and use it in GitHub Desktop.
Promise utilities
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
import Promise from 'bluebird' | |
Promise.serial = (input, initialPromise) => | |
Promise.reduce( | |
input, | |
(previousResult, fn) => fn(previousResult), | |
initialPromise | |
); | |
Promise.prototype.serial = function serial(input) { | |
return Promise.serial(input, this); | |
} | |
/* | |
const getUserName = id => | |
Promise.serial([ | |
response => Promise.resolve(response.json), | |
json => json.userName, | |
], fetch(`https://example.com/user/${id}`)) | |
getUserName(0).then(userName => console.log(userName)); // => jack | |
*/ | |
/* | |
const getUserName = id => | |
fetch(`https://example.com/user/${id}`) | |
.serial([ | |
response => Promise.resolve(response.json), | |
json => json.userName, | |
]); | |
getUserName(0).then(userName => console.log(userName)); // => jack | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment