Skip to content

Instantly share code, notes, and snippets.

@fnky
Last active January 24, 2018 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fnky/33e9ad34504da9a9e852c5cbf2e80830 to your computer and use it in GitHub Desktop.
Save fnky/33e9ad34504da9a9e852c5cbf2e80830 to your computer and use it in GitHub Desktop.
Promise utilities
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