0

I am trying to execute following array (avoid callbackHell) of functions(sync/async), in a sequential order, implementing function runCallbacksInSequence (I need to implement my own function to understand how callbacks work and avoid using Async.js).

Here is what I have so far. The function runCallbacksInSequence works well till it gets the same callback cb(null, 'one'); the second time. Ideally if it gets the same callback more than once it should not execute it second time and ignore it.

If you have any ideas let me know how I can implement it. - no promises and async/await

function first(cb) {
  setTimeout(function() {
    console.log('first()');
    cb(null, 'one');
    cb(null, 'one');//not supposed to be execute by runCallbacksInSequence
  }, 0);
}

function last(cb) {
  console.log('last()');
  cb(null, 'lastCall');
}

function runCallbacksInSequence(fns, cb) {
  fns.reduce(
    (r, f) => {
      return k => {
        return r(() => {
          return f((e, x) => {
            return e ? cb(e) : k(x);
          });
        });
      };
    },
    k => {
      return k();
    }
  )(r => {
    return cb(null, r);
  });
}

fns = [first, last];

runCallbacksInSequence(fns, function(err, results) {
  if (err) return console.log('error: ' + err.message);
  console.log(results);
});
John John
  • 1,305
  • 2
  • 16
  • 37
  • 3
    Possible duplicate of [How to handle a duplicate callback?](https://stackoverflow.com/questions/56520594/how-to-handle-a-duplicate-callback) – depperm Jun 11 '19 at 20:32
  • @depperm a bit different issue – John John Jun 11 '19 at 20:33
  • 1
    Besides posting exaclty, word-for-word, duplicate - what is the different issue? – Randy Casburn Jun 11 '19 at 20:34
  • @RandyCasburn `const fns = [first, second, second, third, last];` vs `function last(cb) { console.log('last()'); cb(null, 'lastCall'); cb(null, 'lastCall'); }` – John John Jun 11 '19 at 20:42
  • 1
    Just as an aside, the `runCallbacksInSequence()` function is impossible to understand for me. Four nested functions inside a `.reduce()`? There's no way for me to imagine what the function is trying to do. You might want to simplify your code (or explain it better) in order to get some help. – Aioros Jun 11 '19 at 20:45
  • @Aioros good point . I will try to update the original question – John John Jun 11 '19 at 20:49
  • 1
    Avoiding callback hell equals: return .. return ... return ... return...return...and...return? – Randy Casburn Jun 11 '19 at 20:51
  • @RandyCasburn exactly it is rebuilding the same callback hell but programmatically, so you do not have to do it by yourself every time when you need sequential order – John John Jun 11 '19 at 20:55
  • 2
    The nested functions might be a little easier to understand if the parameters had meaningful names. – Barmar Jun 11 '19 at 20:55
  • @Barmar good point . I will try to update the original question – John John Jun 11 '19 at 20:58
  • You really should simply use `reduceRight` instead of transforming the left fold to a right fold with CPS. – Bergi Jun 11 '19 at 21:00
  • "*how I can implement it*" - you will simply need a boolean flag to store and check whether the function has already been called – Bergi Jun 11 '19 at 21:01
  • 3
    [Are](https://stackoverflow.com/questions/56422248/promise-based-sequence-converting-into-callback-based) [these](https://stackoverflow.com/questions/56471930/executing-callbacks-in-sequential-order) [all](https://stackoverflow.com/questions/56473032/passing-the-result-of-the-callback-down-the-chain-in-sequential-order-to-the-out) [really](https://stackoverflow.com/questions/56488047/error-handling-executing-callbacks-in-sequential-order) [different](https://stackoverflow.com/questions/56490295/) [questions](https://stackoverflow.com/questions/56520594/)? – Herohtar Jun 11 '19 at 21:03
  • @Herohtar really different – John John Jun 11 '19 at 21:03
  • @Bergi continuation-passing style (CPS) ? – John John Jun 11 '19 at 21:04
  • @Bergi you mean some type of `let cache = { }` but within the scope of the `callback` ? – John John Jun 11 '19 at 21:05
  • @JohnJohn Yes, yes. – Bergi Jun 11 '19 at 21:11
  • @Bergi I will play with it. Thx! – John John Jun 11 '19 at 21:14
  • You should also consider studying the async.js code if that is what you want to implement... – Bergi Jun 11 '19 at 21:17
  • @Bergi I tried to look at it but was not able to trace it `async/lib/series.js` – John John Jun 11 '19 at 21:20
  • @JohnJohn You might want to start with a really old version of the library, where the code was not that high-level yet. Although judging from the code in your question, you shouldn't have problems with functional style... – Bergi Jun 11 '19 at 21:23
  • @Bergi good point, thx! – John John Jun 11 '19 at 21:25

0 Answers0