[英]Wait for save before continuing with the function
I have a loop which loops through an array of users, this loop is asynchronous, what I want to do is for every user create a new document and save it and wait for this to save and then move on to the next user. 我有一个循环遍历一系列用户的循环,该循环是异步的,我要为每个用户创建一个新文档并保存并等待其保存,然后移至下一个用户。 I am able to handle when to move on to the next user, so my problem is while saving the new document data. 我能够处理何时移至下一个用户,所以我的问题是在保存新文档数据时。 I am using mongoose save method. 我正在使用猫鼬保存方法。
The main function is running the loop, here is a sample. 主要功能是运行循环,这是一个示例。 NOTE: 'async' is the async module v2.6.0 注意:“异步”是异步模块v2.6.0
async.forEach(users, (user, callback)=>{
console.log(`Checking ${user.first_name}`);
createNewDocument(user, (err)=>{
if(!err) {
console.log("New document created.");
callback();
}
}, (err)=>{
if(!err){
console.log("All users were updated");
}
});
}
The non-async method 非异步方法
users.forEach((user)=>{
console.log(`Checking ${user.first_name}`);
createNewDocument(user, (err)=>{
if(!err) {
console.log("New document created.");
}
});
});
The createNewDocument function createNewDocument函数
function createNewDocument(user, callback)
{
console.log("Creating a new doc...");
let doc = new Doc();
doc.save((err)=>{
if(!err){
console.log(`New doc created for user ${user.first_name}`);
callback;
}
});
console.log("Creating a new doc finished executing...");
}
Everything is executed and the last thing I get in my log is bunch of "New doc created for user...." for all the users. 一切都已执行,我在日志中得到的最后一件事是为所有用户创建了“为用户创建的新文档...。” I have tried quite a lot of stuff, I tried putting the doc.save in a promise but that didn't work as well, not sure what should I do now. 我已经尝试了很多东西,我尝试将doc.save放在一个诺言中,但是那也不起作用,不确定现在该怎么办。
EDIT: It's important for me to create the new document before moving on to the next user, I don't want to add them to calls array and then execute it, I need to make a check that the previous document actually exist and take some data from it on the next time the loop enters 编辑:对于我来说,在移至下一个用户之前创建新文档很重要,我不想将它们添加到调用数组然后执行它,我需要检查上一个文档是否确实存在并接受一些下次进入循环时从中获取数据
There have been promises for nested async and proper error handling for years already. 嵌套异步和正确的错误处理已经有很多年了。 Recently there are the async
and await
syntax to make promises look like C# code but the important thing is to learn how promises work because that syntax uses promises. 最近,有async
和await
语法来使Promise看起来像C#代码,但重要的是要学习Promise如何工作,因为该语法使用Promise。
Using promises you can do the following: 使用诺言,您可以执行以下操作:
const createNewDocument = user =>
new Promise(
(resolve,reject) => {
console.log("Creating a new doc...");
let doc = new Doc();
doc.save(
err=>{
console.log("Creating a new doc finished executing...");
err
? reject(err)
: resolve(`New doc created for user ${user.first_name}`)
}
);
}
);
const saveDocuments = (users,processedSoFar=[]) => {
if(users.length===0){
return processedSoFar;
}
console.log(`Checking ${users[0].first_name}`);
return createNewDocument(users[0])
.then(
result=>{
console.log("New document created.");
processedSoFar.push(users[0]);
return saveDocuments(users.slice(1),processedSoFar);
}
)
.catch(
err=>Promise.reject([err,processedSoFar])
);
}
saveDocuments(users)
.then(
result=>{
console.log("Processed the following users:",users);
}
)
.catch(
([err,processed])=>{
console.log(
"Something went wrong:",err,
"Processed so far:",processed
)
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.