Skip to content

Instantly share code, notes, and snippets.

@jasonrhodes
Last active March 30, 2018 18:02
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 jasonrhodes/0650a25758719b6e765f59629fc56163 to your computer and use it in GitHub Desktop.
Save jasonrhodes/0650a25758719b6e765f59629fc56163 to your computer and use it in GitHub Desktop.
// You can chain a list of this type of "chainable" http function with "callback hell"
chainA({ cb: (results) =>
chainB({ results, cb: () =>
chainC({ cb: (results) =>
chainD({ results })
})
})
})
// But I want a helper with this API:
chain(chainA, chainB, chainC, chainD)
import { http } from './http'
// example of a "chainable" http method
function isChainable({ results, cb, ...rest }) {
const args = {} // may or may not use results and/or rest to construct http args
args.cb = cb;
http(args)
}
// the baseline magic http method, e.g.
export function http(args) {
// does stuff with args
return makeHttpRequest(args)
.then((results) => {
if (args.cb) {
cb(results)
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment