问题描述
假设我在下面有这个 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
});
我想做的是将继续代码 (在传递给async.parallel
的所有函数完成后继续/运行的代码)放在{{1 }}。
我试过了:
async.parallel
还有这个:
const results = await async.parallel({
one: async (callback) => {/* ... some async code */ return someResult},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},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"
并成功获取结果对象?
建议的解决方案/替代方案: (非还不是我的问题的解决方案,但它们可能是有用的临时替代方案)
备选方案 1: (我不喜欢,因为它看起来比 async.parallel 更丑/麻烦)
async.parallel
关于备选方案 1 的说明:
为了使方案 1 更简单,我们可以创建 const results = await Promise.all([
new Promise( (resolve,reject) => {/* ... some async code */ resolve('some result 1')} ),new Promise( (resolve,reject) => {/* ... some async code */ resolve('some result 2')} ),reject) => {/* ... some async code */ resolve('some result 3')} )
]);
debug(results); // prints "results" object successfully without problems
:
promisify(some_function)
进一步简化备选方案 1:
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;
}
我们最终可能会构建一个与异步库类似的库并重新发明轮子,特别是当我们需要 concat、waterfall 等功能时。
解决方法
由于您的数据库的现代版本已经支持 promise,因此您不需要 async
库来执行此操作。相反,您可以这样做:
let results = await Promise.all([
Car_model.findOne(...),Car_model.findOne(...),Car_model.findOne(...)
]);
console.log(results);
当然,此代码需要位于 async
函数内,以便您可以使用 await
。
去掉回调函数,async.parallel 会返回一个Promise。
async.parallel 返回一个 promise,如果没有传递回调。
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},});
debug(results) // prints "results" object successfully without problems
为了处理错误,将代码放在 try..catch
中:
try {
let results = await async.parallel({
one: async (callback) => {/* ... some async code */ return someResult},});
debug(results);
} catch(err) {
/* … error handling code */
}