A small concept library that allows clear separation between side-effect functions and pure functions in Promise chains.
See this short article to learn about what promise-passthrough attempts to solve.
npm install promise-passthrough --save
- javascript es6
- or a globally available Promise (maybe this polyfill)
import { passThrough } from 'promise-passthrough';
const cacheData = (response) => {
cacheStore.put('user', response);
return undefined;
}
const parseUserResponse = (response) => {
return response.data.user;
}
const updateLocalDatabase = (user) => {
localDB.update('user', user);
return undefined;
}
const refreshUserCreditCards = (user) => {
wallet.update('credits', user.credits)
return undefined;
}
httpClient.get('https://facebook.com/user/' + id)
.then(passThrough(cacheData))
.then(responseToUser)
.then(passThroughAwait(updateLocalDatabase))
.then(passThrough(refreshUserCredits))
.then((user) => {
// Even though 'updateLocalDatabase' and 'refreshUserCredits'
// both return undefined, by wrapping them in the
// passThrough/passThroughAwait functions, the user object is
// ensured to be returned and fed into the next line
// of the Promise chain.
console.log(`Hello ${user.name}`);
});
MIT © Gabriel C. Troia