给定以下 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
添加到提供给 one
和 two
函数的参数列表中通过 async.series
。
请注意,我还在回调中移动了结果和 next()
处理,因为这可能是您想要的。
关于node.js - 如何将一组标准参数传递给 async.js 系列中的每个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14305843/