我可以 return 从函数内部的 npm async parallel 得到结果吗
Can I return results from npm async parallel inside a function
我想 return nodejs 中 npm async 的结果,当我调用函数 'findRequestAgent'
时,它 return 未定义
findRequestAgent : (agentId) => {
var queries = [];
var datum;
queries.push((callback) => {
MovingAgent.findById(agentId,(err,doc) => {
if (err) {
console.log("There was an error finding the moving agent");
throw err;
}
callback(null, doc)
})
});
queries.push((callback) => {
SelfStander.findOne({ user_id: agentId}, (err, doc) => {
if (err) {
console.log("There was an error finding the self stander");
throw err;
}
callback(null, doc)
})
});
queries.push((callback) =>{
MovingCompany.findOne({custom_id: agentId}, (err, doc) => {
if (err) {
console.log("There was an error finding the self stander");
throw err;
}
callback(null, doc);
})
});
async.parallel(queries, (err, results) => {
if (err) {
console.log("There was an error perfoming async");
throw err;
}
datum = results;
console.log(datum);
});
return datum;
}
我该怎么做才能在调用上述函数时 'findRequestAgent' 它 return 结果
作为一般规则,异步函数不会直接 return 值。您需要使用其他机制,例如回调、事件、承诺。
如果不进一步了解您的用例,就很难知道正确的解决方案是什么。但是在继续之前,您绝对应该停下来并确保您了解异步函数在 JavaScript 中的工作原理。
最简单的解决方案是在传递给 .parallel()
的回调中执行您需要执行的操作,但同样,在不了解更多信息的情况下,不能肯定地说这对您的用例有效.
所有 Mongo 查询 return 承诺。
var queries = [];
queries.push(
MovingAgent.findById(agentId,(err,doc) => {
if (err) {
console.log("There was an error finding the moving agent");
throw err;
}
return (null, doc);
})
);
...
return Promise.all(queries).then(
(results) => results; //array of results
).catch(
(err) => console.log(err);
)
不需要异步部分
我想 return nodejs 中 npm async 的结果,当我调用函数 'findRequestAgent'
时,它 return 未定义findRequestAgent : (agentId) => {
var queries = [];
var datum;
queries.push((callback) => {
MovingAgent.findById(agentId,(err,doc) => {
if (err) {
console.log("There was an error finding the moving agent");
throw err;
}
callback(null, doc)
})
});
queries.push((callback) => {
SelfStander.findOne({ user_id: agentId}, (err, doc) => {
if (err) {
console.log("There was an error finding the self stander");
throw err;
}
callback(null, doc)
})
});
queries.push((callback) =>{
MovingCompany.findOne({custom_id: agentId}, (err, doc) => {
if (err) {
console.log("There was an error finding the self stander");
throw err;
}
callback(null, doc);
})
});
async.parallel(queries, (err, results) => {
if (err) {
console.log("There was an error perfoming async");
throw err;
}
datum = results;
console.log(datum);
});
return datum;
}
我该怎么做才能在调用上述函数时 'findRequestAgent' 它 return 结果
作为一般规则,异步函数不会直接 return 值。您需要使用其他机制,例如回调、事件、承诺。
如果不进一步了解您的用例,就很难知道正确的解决方案是什么。但是在继续之前,您绝对应该停下来并确保您了解异步函数在 JavaScript 中的工作原理。
最简单的解决方案是在传递给 .parallel()
的回调中执行您需要执行的操作,但同样,在不了解更多信息的情况下,不能肯定地说这对您的用例有效.
所有 Mongo 查询 return 承诺。
var queries = [];
queries.push(
MovingAgent.findById(agentId,(err,doc) => {
if (err) {
console.log("There was an error finding the moving agent");
throw err;
}
return (null, doc);
})
);
...
return Promise.all(queries).then(
(results) => results; //array of results
).catch(
(err) => console.log(err);
)
不需要异步部分