node.js - 如何将一组标准参数传递给 async.js 系列中的每个函数?

标签 node.js middleware async.js

给定以下 node.js 模块,我如何调用数组 orderedListOfFunctions 中的函数,将每个函数传递给 response 变量?

var async = require("async");
var one = require("./one.js");
var two = require("./two.js");

module.exports = function (options) {

var orderedListOfFunctions = [
    one,
    two
];

return function (request, response, next) {

    // This is where I'm a bit confused...
    async.series(orderedListOfFunctions, function (error, returns) {
        console.log(returns);
        next();
    });

};

最佳答案

您可以使用 bind这样做:

module.exports = function (options) {
  return function (request, response, next) {
    var orderedListOfFunctions = [
      one.bind(this, response),
      two.bind(this, response)
    ];

    async.series(orderedListOfFunctions, function (error, resultsArray) {
      console.log(resultArray);
      next();
    });
};

bind 调用在调用时将 response 添加到提供给 onetwo 函数的参数列表中通过 async.series

请注意,我还在回调中移动了结果和 next() 处理,因为这可能是您想要的。

关于node.js - 如何将一组标准参数传递给 async.js 系列中的每个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14305843/

相关文章:

node.js - 我怎样才能在这个服务器上放置一个循环

javascript - 有没有一种简单可靠的方法可以使用 Electron/Node.js/terminal 通过本地网络传输文件?

node.js - 使用 mongoosastic 进行自动完成

javascript - NPM 异步系列问题

javascript - Electron require() 未定义

python - FastAPI 保存上下文将在端点中可用

express - 如何在 NestJS 中安装 Express 中间件(express-openapi-validator)?

node.js - 访问 Express JS/Connect JS 中间件内部的 "app"变量?

javascript - 为什么 async.forEach 回调永远不会被调用?

javascript - 如何使用 async.js 映射数组?