Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // First approach: pass functions as arguments for chaining. Executes from left to right;
- // Please, run with node >= 8 (because of asserts)
- function chain() {
- let funcs = [...arguments];
- let finish;
- function run(acc) {
- if (funcs.length > 0) {
- const func = funcs.shift();
- if (typeof func === 'function') {
- func(acc, (err, res) => {
- if (err) {
- finish(err);
- } else {
- run.call(null, res);
- }
- });
- }
- } else {
- finish(null, acc);
- }
- }
- return function (number, cb) {
- finish = cb;
- run(number);
- }
- }
- var shouldBeTwelve = chain(plusTwo, timesTwo, plusTwo);
- shouldBeTwelve(3, (err, result) => {
- assert.equal(result, 12)
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement