Skip to content

A little function to ensure data flows correctly through the promise chain, while abstracting the logic away from implementation.

Notifications You must be signed in to change notification settings

GabrielCTroia/promise-passthrough

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Promise Passthrough

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.

Install

npm install promise-passthrough --save

Dependencies

  • javascript es6
  • or a globally available Promise (maybe this polyfill)

Usage

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}`);
  });

Licence

MIT © Gabriel C. Troia

About

A little function to ensure data flows correctly through the promise chain, while abstracting the logic away from implementation.

Resources

Stars

Watchers

Forks

Packages

No packages published