我有一个异步函数,其中有两个等待函数。第二个似乎没有等待第一个。
TBL_USERS
我试图为第一个功能submitForm(req, res) {
/**
* @param {Object} auth the authentication generated from authorize function.
*/
const uploadFilesToDrive = async auth => {
await drive.files.create(
{
resource: folderMetadata,
fields: "id"
},
(err, folder) => {
if (err) {
console.error(err);
} else {
for (let elem in req.files) {
file = bufferToStream(req.files[elem][0].buffer);
fileName = req.files[elem][0].originalname;
mimeType = req.files[elem][0].mimetype;
fileMetadata = {
name: fileName,
parents: [folder.data.id]
};
media = {
mimeType,
body: file
};
drive.files.create(
{
resource: fileMetadata,
media,
fields: "id"
},
(err, file) => {
if (err) {
console.error(err);
} else {
form[
req.files[elem][0].fieldname
] = `https://drive.google.com/open?id=${file.data.id}`;
}
}
);
}
}
}
);
await Form.create(form)
.catch(err => {
console.log(err);
res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
message: err
});
})
.then(form => {
res.status(HttpStatus.CREATED).json({
message: "Form submitted successfully",
form
});
});
};
loadDriveSecret(uploadFilesToDrive);
}
创建一个诺言,并在循环await new Promise((resolve, reject) => drive.files.create(...
之后立即解决它,但这仍然没有等待。我在控制台上看到,在for循环仍在运行的情况下,第一个函数将立即解析并转到下一个函数。