I am trying to convert Promise based runPromiseInSequence
into callback based function. Here is what I have so far. I do not quite understand how callbacks work tha
[f1, f2, f3, f4].reduce((promise, f) => promise.then(f), Promise.resolve(10))
in your runPromiseInSequence
basically boils down to
Promise.resolve(10).then(f1).then(f2).then(f3).then(f4);
This pattern doesn't work with callbacks, as there is no value to start with that we could chain on. Instead, we need to nest:
f1(10, r1 => f2(r1, r2 => f3(r2, r3 => f4(r3, r4 => cb(r4))));
This can be achieved using reduceRight
:
[f1, f2, f3, f4].reduceRight((cb, f) => r => f(r, cb), cb)(10)