Last active
March 30, 2018 18:02
-
-
Save jasonrhodes/0650a25758719b6e765f59629fc56163 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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) |
This file contains hidden or 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 { 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) | |
} |
This file contains hidden or 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
// 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