Javascript 如何等待async.parallel?
假设我有下面的Javascript 如何等待async.parallel?,javascript,node.js,asynchronous,async-await,async.js,Javascript,Node.js,Asynchronous,Async Await,Async.js,假设我有下面的async.parallel代码示例,并行运行异步函数并等待它们完成,然后对结果运行代码 async.parallel({ one: async (callback) => {/* ... some async code */ return someResult}, two: async (callback) => {/* ... some async code */ return someResult}, three: async (callback) =
async.parallel
代码示例,并行运行异步函数并等待它们完成,然后对结果运行代码
async.parallel({
one: async (callback) => {/* ... some async code */ return someResult},
two: async (callback) => {/* ... some async code */ return someResult},
three: async (callback) => {/* ... some async code */ return someResult},
},(err,results) => {
if (err) { /* … error handling code */ }
/* … continued code */
debug(results) // prints "results" object successfully without problems
});
我想做的是将continued code(在传递给async.parallel
的所有函数完成后继续/运行的代码)放到async.parallel
之外
我试过这个:
const results = await async.parallel({
one: async (callback) => {/* ... some async code */ return someResult},
two: async (callback) => {/* ... some async code */ return someResult},
three: async (callback) => {/* ... some async code */ return someResult},
},(err,results) => {
if (err) { /* … error handling code */ }
debug("test print1: from inside async.parallel callback function");
// called/printed after "test print2" meaning that async.parallel isn't awaited.
return results;
});
/* … continued code */
debug("test print2: from outside async.parallel");
// printed before "test print1" meaning that async.parallel isn't awaited.
debug(results) // prints "undefined"
这是:
let results_outer;
await async.parallel({
one: async (callback) => {/* ... some async code */ return someResult},
two: async (callback) => {/* ... some async code */ return someResult},
three: async (callback) => {/* ... some async code */ return someResult},
},(err,results) => {
if (err) { /* … error handling code */ }
debug("test print1: from inside async.parallel callback function");
// printed after "test print2" meaning that async.parallel isn't awaited.
results_outer = results;
});
/* … continued code */
debug("test print2: from outside async.parallel");
// printed before "test print1" meaning that async.parallel isn't awaited.
debug(results_outer) // prints "undefined"
我的问题是,如何等待async.parallel
并成功地获取对象结果
建议的解决方案/备选方案:(非仍然是我问题的解决方案,但它们可能是有用的临时备选方案) 备选方案1:(我不喜欢,因为它看起来比async.parallel更难看/麻烦) 关于备选方案1的注释:
为了简化备选方案1,我们可以创建
promisify(一些函数)
:const results = await Promise.all([
promisfy(some_functionOne),
promisfy(some_functionTwo),
promisfy(some_functionThree)
]);
debug(results); // prints "results" object successfully without problems
function promisfy(some_function) {
const promise = new Promise( (resolve, reject) => {
some_function_return_value = await some_function();
resolve(some_function_return_value);
} );
return promise;
}
为了进一步简化备选方案1:
async_parallel([some_functionOne, some_functionTwo, some_functionThree]);
debug(results); // prints "results" object successfully without problems
function async_parallel(functions_to_run) {
let promises = functions_to_run.map(function_to_run => promisfy(function_to_run));
const results = await Promise.all(promises);
return results;
}
我们最终可能会构建一个类似于async库的库,并重新发明轮子,特别是当我们需要诸如等功能时。由于您的数据库的现代版本已经支持promises,因此您不需要使用
async
库来实现这一点。相反,您可以这样做:
let results = await Promise.all([
Car_model.findOne(...),
Car_model.findOne(...),
Car_model.findOne(...)
]);
console.log(results);
当然,这段代码需要在
async
函数中,这样您就可以使用wait
因为现代版本的数据库已经支持承诺,所以您不需要async
库来完成这项工作。相反,您可以这样做:
let results = await Promise.all([
Car_model.findOne(...),
Car_model.findOne(...),
Car_model.findOne(...)
]);
console.log(results);
当然,这段代码需要在
async
函数中,这样您就可以使用wait
删除回调函数,async.parallel将返回一个承诺
如果未传递回调,async.parallel将返回承诺
要处理错误,请将代码放入try..catch
:
try {
let results = await async.parallel({
one: async (callback) => {/* ... some async code */ return someResult},
two: async (callback) => {/* ... some async code */ return someResult},
three: async (callback) => {/* ... some async code */ return someResult},
});
debug(results);
} catch(err) {
/* … error handling code */
}
只需删除回调函数,async.parallel将返回一个承诺 如果未传递回调,async.parallel将返回承诺 要处理错误,请将代码放入
try..catch
:
try {
let results = await async.parallel({
one: async (callback) => {/* ... some async code */ return someResult},
two: async (callback) => {/* ... some async code */ return someResult},
three: async (callback) => {/* ... some async code */ return someResult},
});
debug(results);
} catch(err) {
/* … error handling code */
}
看看下面,这可能会有所帮助。为什么不干脆
let results=wait Promise.all([Car_model.findOne(…)、Car_model.findOne(…)、Car_model.findOne(…)、Car_model.findOne(…))代码>?由于您的数据库已经支持promises,我不明白为什么需要async
库来实现这一点。这些是mongoose模型吗?您可以使用promise API而不是async吗?由于Javascript语言中promises和async/await
的发展以及支持promises的NodeJ中越来越多的库和内置函数,使用async
或Bluebird
这样的库的理由越来越少了。是的,仍然存在一些情况,例如在执行大量并行操作时管理并发请求的数量,但大多数情况下,您不再需要这些库,最好只使用现代Javascript提供的工具进行编程。不,最初的async
库出现在Promises之前,旨在管理非promise、普通回调异步操作。自那以后,它已经被修改为包含承诺,但它最初设计的许多功能现在都可以在不使用常规承诺的流控制的情况下完成,特别是当您包含async/await
时。看看下面,这可能会有所帮助。为什么不干脆let results=wait Promise.all([Car_model.findOne(…)、Car_model.findOne(…)、Car_model.findOne(…)、Car_model.findOne(…))代码>?由于您的数据库已经支持promises,我不明白为什么需要async
库来实现这一点。这些是mongoose模型吗?您可以使用promise API而不是async吗?由于Javascript语言中promises和async/await
的发展以及支持promises的NodeJ中越来越多的库和内置函数,使用async
或Bluebird
这样的库的理由越来越少了。是的,仍然存在一些情况,例如在执行大量并行操作时管理并发请求的数量,但大多数情况下,您不再需要这些库,最好只使用现代Javascript提供的工具进行编程。不,最初的async
库出现在Promises之前,旨在管理非promise、普通回调异步操作。自那以后,它已经被修改为包含承诺,但它最初设计的许多功能现在都可以不用使用常规承诺的流控制来完成,特别是当您包含async/await
时。我在线程上给出了相同的答案。是的,它在某种程度上是相似的,但是我不清楚如何使用await
。我在线程中给出了相同的答案。是的,它在某种程度上是相似的,但是我不清楚如何使用await
。