在不使用 Promise 的情况下按顺序执行回调 [英] Executing callbacks in sequential order without using Promises

查看:26
本文介绍了在不使用 Promise 的情况下按顺序执行回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按顺序执行以下函数数组(避免 callbackHell)以实现函数 runCallbacksInSequence(我需要实现自己的函数以了解回调如何工作并避免使用 Async.js).这是我到目前为止所拥有的.我不太明白回调是如何工作的,这就是我做这个练习的原因.如果您有任何想法,请告诉我我做错了什么以及如何解决.

I am trying to execute following array (avoid callbackHell) of functions 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. I do not quite understand how callbacks work that is why I am doing this exercise. If you have any ideas let me know what I am doing wrong and how I can fix it.

function first(cb) {
  console.log('first()');
  cb();
}
function second(cb) {
  console.log('second()');
  cb();
}
function third(cb) {
  console.log('third()');
  cb();
}
function last() {
  console.log('last()');
}

let fns = [first, second, third, last];

function runCallbacksInSequence(fns, cb) {
  return fns.reduceRight((acc, f) => f(acc), cb);
}

runCallbacksInSequence(fns, second);

回调地狱

// first(function() {
//   third(function() {
//     second(function() {
//       last();
//     });
//   });
// });

UPD

    const cache = {};

    function runCallbacksInSequence(fns, cb) {
      fns.reduce(
        function(r, f) {
          return function(k) {
            return r(function() {
              if (cache[f]) {
                return;
                // f(function(e, x) {
                //   e ? cb(e) : k(x);
                // });
              } else {
                cache[f] = f;
                return f(function(e, x) {
                  return e ? cb(e) : k(x);
                });
              }
            });
          };
        },
        function(k) {
          return k();
        }
      )(function(r) {
        return cb(null, r);
      });
    }

推荐答案

.reduce 回调成为一个 高阶 函数,该函数在调用时调用带有回调的链中的下一个函数.最后,您将拥有一个函数链,首先调用第一个函数,然后调用第二个函数,依此类推:

Have the .reduce callback be a higher-order function, which, when called, calls the next function in the chain with the callback. At the end, you'll have a function chain that will start by calling the first function, then the second, etc:

function first(cb) {
  console.log('first()');
  cb();
}
function second(cb) {
  console.log('second()');
  cb();
}
function third(cb) {
  console.log('third()');
  cb();
}
function last() {
  console.log('last()');
}

let fns = [first, second, third, last];

function runCallbacksInSequence(fns, cb) {
  const chainedFns = fns.reduceRight((acc, f) => () => f(acc), cb);
  return chainedFns();
}

runCallbacksInSequence(fns);

如果您希望 runCallbacksInSequence 接受 另一个 回调以在所有回调结束时运行,则:

If you wanted the runCallbacksInSequence to accept another callback to run at the end of all of them, then:

function first(cb) {
  console.log('first()');
  cb();
}
function second(cb) {
  console.log('second()');
  cb();
}
function third(cb) {
  console.log('third()');
  cb();
}
function last(cb) {
  console.log('last()');
  cb();
}

let fns = [first, second, third, last];

function runCallbacksInSequence(fns, cb) {
  const chainedFns = fns.reduceRight((acc, f) => () => f(acc), cb);
  return chainedFns();
}

runCallbacksInSequence(fns, () => console.log('outer call'));

这篇关于在不使用 Promise 的情况下按顺序执行回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆