执行函数异步系列
Executes functions async series
我正在尝试使用 node.JS 执行两个异步系列函数。
但是我不明白这样做。
现在,我有:
函数 1:
function search(client_id, callback) {
clientRedis.keys('director:*', function (err, results) {
results.forEach(function (key) {
clientRedis.hgetall(key, function (err, obj) {
//SAVE RESULT
console.log('save');
});
});
});
callback(null, results);
}
函数 2:
function range(client_id, callback) {
//Sort my array
callback(null, results);
}
我在这里调用这个函数:
async.series([
search(client_id),
range(client_id);
], function (err, result) {
console.log(err);
console.log(result);
});
我的问题:第二个函数在第一个之前执行,因为第一个需要更多时间。我需要第一个函数的结果来使用函数 2.
来排列我的数组
search(client_id)
和 range(client_id)
将立即执行,将 callback
参数分配给 undefined
然后 async.series
将尝试执行这些函数的结果一个系列,可能会失败,因为它们不是函数,而是 undefined
。或者更确切地说,如果函数没有尝试执行 undefined(null, results)
.
请记住,如果 f
是一个函数,f(...)
会执行它。您需要将函数本身传递给 async.series
,而不是它们的执行结果。
async.series
希望您传入任务数组或对象,每个任务都是 function(callback) { ... }
.
因此,以下内容应该有所帮助:
async.series([
function(callback) { search(client_id, callback); },
function(callback) { range(client_id, callback); }
]...)
如果您使用 Haskell 编写支持柯里化的代码,那么您的代码就是正确的;但在 JavaScript 中,f(x)(y)
与 f(x, y)
不同。
您也不要从 Redis 成功函数内部调用 callback
,这也会打乱您的时间安排。
您应该使用 async.waterfall
而不是 async.series
以在第二个函数中获得第一个函数结果。
Runs the tasks array of functions in series, each passing their results to the next in the array. However, if any of the tasks pass an error to their own callback, the next function is not executed, and the main callback is immediately called with the error.
而且,你的代码有很大的错误。如果我理解你的代码,你想在修改所有结果后转到第二个函数并且 return 这是第二个函数,对吗?在这种情况下,使用 async.each
而不是 result.forEach
并在异步之后调用回调 each :
function search(client_id, callback) {
clientRedis.keys('director:*', function (err, results) {
var savedElems = [];
async.each(results, function (key, done) {
clientRedis.hgetall(key, function (err, obj) {
if (err) {
return done(err);
}
savedElems.push(obj);
done();
});
}, function (err) {
if (err) {
return callback(err);
}
callback(null, savedElems);
});
});
}
如果您不打算直接在第二个函数中使用第一个函数的结果(仅通过 redis),您可以使用如下方法:
async.series([
search.bind(null, client_id),
range.bind(null, client_id)
], function (err, results) {
console.log(err);
console.log(results[0]); // for search results
console.log(results[1]); // for range results
});
我正在尝试使用 node.JS 执行两个异步系列函数。 但是我不明白这样做。
现在,我有:
函数 1:
function search(client_id, callback) {
clientRedis.keys('director:*', function (err, results) {
results.forEach(function (key) {
clientRedis.hgetall(key, function (err, obj) {
//SAVE RESULT
console.log('save');
});
});
});
callback(null, results);
}
函数 2:
function range(client_id, callback) {
//Sort my array
callback(null, results);
}
我在这里调用这个函数:
async.series([
search(client_id),
range(client_id);
], function (err, result) {
console.log(err);
console.log(result);
});
我的问题:第二个函数在第一个之前执行,因为第一个需要更多时间。我需要第一个函数的结果来使用函数 2.
来排列我的数组search(client_id)
和 range(client_id)
将立即执行,将 callback
参数分配给 undefined
然后 async.series
将尝试执行这些函数的结果一个系列,可能会失败,因为它们不是函数,而是 undefined
。或者更确切地说,如果函数没有尝试执行 undefined(null, results)
.
请记住,如果 f
是一个函数,f(...)
会执行它。您需要将函数本身传递给 async.series
,而不是它们的执行结果。
async.series
希望您传入任务数组或对象,每个任务都是 function(callback) { ... }
.
因此,以下内容应该有所帮助:
async.series([
function(callback) { search(client_id, callback); },
function(callback) { range(client_id, callback); }
]...)
如果您使用 Haskell 编写支持柯里化的代码,那么您的代码就是正确的;但在 JavaScript 中,f(x)(y)
与 f(x, y)
不同。
您也不要从 Redis 成功函数内部调用 callback
,这也会打乱您的时间安排。
您应该使用 async.waterfall
而不是 async.series
以在第二个函数中获得第一个函数结果。
Runs the tasks array of functions in series, each passing their results to the next in the array. However, if any of the tasks pass an error to their own callback, the next function is not executed, and the main callback is immediately called with the error.
而且,你的代码有很大的错误。如果我理解你的代码,你想在修改所有结果后转到第二个函数并且 return 这是第二个函数,对吗?在这种情况下,使用 async.each
而不是 result.forEach
并在异步之后调用回调 each :
function search(client_id, callback) {
clientRedis.keys('director:*', function (err, results) {
var savedElems = [];
async.each(results, function (key, done) {
clientRedis.hgetall(key, function (err, obj) {
if (err) {
return done(err);
}
savedElems.push(obj);
done();
});
}, function (err) {
if (err) {
return callback(err);
}
callback(null, savedElems);
});
});
}
如果您不打算直接在第二个函数中使用第一个函数的结果(仅通过 redis),您可以使用如下方法:
async.series([
search.bind(null, client_id),
range.bind(null, client_id)
], function (err, results) {
console.log(err);
console.log(results[0]); // for search results
console.log(results[1]); // for range results
});