Javascript 两个不同文件中的两个异步代码。我希望他们一个接一个地跑,但他们同时跑
第二个文件中的代码取决于第一个文件,因此我希望第一个文件代码首先执行,当第一个文件代码完成执行时,第二个文件中的代码应该运行。第一个文件中的代码基本上将图像存储在google cloud bucket中,然后返回该图像的URL,第二个文件更新MongoDB中的post模式,并将URL字符串推送到images数组中。由于两个文件中的代码并排运行,并且第二个文件中的代码不会等待第一个文件执行post模式,因此即使在第一个代码返回所有字符串url之前,post模式也会更新,这是一个问题。我想先获取所有字符串url,然后更新post模式 所以在我的第一个文件中Javascript 两个不同文件中的两个异步代码。我希望他们一个接一个地跑,但他们同时跑,javascript,node.js,Javascript,Node.js,第二个文件中的代码取决于第一个文件,因此我希望第一个文件代码首先执行,当第一个文件代码完成执行时,第二个文件中的代码应该运行。第一个文件中的代码基本上将图像存储在google cloud bucket中,然后返回该图像的URL,第二个文件更新MongoDB中的post模式,并将URL字符串推送到images数组中。由于两个文件中的代码并排运行,并且第二个文件中的代码不会等待第一个文件执行post模式,因此即使在第一个代码返回所有字符串url之前,post模式也会更新,这是一个问题。我想先获取所有
function sendUploadsToGCS(req, res, next){
if(!req.files){
return next();
}
req.files.map((image, index) => {
const gcsname = uuid() + image.originalname;
const file = craigFilesBucket.file(gcsname);
const stream = file.createWriteStream({
metadata: {
contentType: image.mimetype,
}
});
stream.on('error', err =>{
req.files[index].cloudStorageError = err;
next(err);
});
stream.on('finish', async () => {
req.files[index].cloudStorageObject = gcsname;
await file.makePublic();
req.files[index].cloudStoragePublicUrl = getPublicUrl(gcsname);
console.log(req.files[index].cloudStoragePublicUrl);
next();
});
stream.end(image.buffer);
})
}
const addImagestoPost = async (req, res, next) =>{
const errors = validationResult(req);
if(!errors.isEmpty()){
return next(new HttpError('Invalid inputs passed, please check your data.', 422))
}
const postId = req.params.pid;
let post;
try{
post = await Post.findById(postId);
}catch(err){
const error = new HttpError(
'Something went wrong could not update post',
500
);
return next(error);
}
// if(!req.files[0] || !req.files[0].cloudStoragePublicUrl){
// return next(new HttpError('cloudStoragePublicUrl err.', 422))
// }
//images: req.files.cloudStoragePublicUrl,
console.log('HEllo World');
try{
await post.save();
}catch(err){
const error = new HttpError(
'Something went wrong, could not update post', 500
)
}
res.status(200).json({post: post.toObject({getters: true})})
}
在我的第二个文件中
function sendUploadsToGCS(req, res, next){
if(!req.files){
return next();
}
req.files.map((image, index) => {
const gcsname = uuid() + image.originalname;
const file = craigFilesBucket.file(gcsname);
const stream = file.createWriteStream({
metadata: {
contentType: image.mimetype,
}
});
stream.on('error', err =>{
req.files[index].cloudStorageError = err;
next(err);
});
stream.on('finish', async () => {
req.files[index].cloudStorageObject = gcsname;
await file.makePublic();
req.files[index].cloudStoragePublicUrl = getPublicUrl(gcsname);
console.log(req.files[index].cloudStoragePublicUrl);
next();
});
stream.end(image.buffer);
})
}
const addImagestoPost = async (req, res, next) =>{
const errors = validationResult(req);
if(!errors.isEmpty()){
return next(new HttpError('Invalid inputs passed, please check your data.', 422))
}
const postId = req.params.pid;
let post;
try{
post = await Post.findById(postId);
}catch(err){
const error = new HttpError(
'Something went wrong could not update post',
500
);
return next(error);
}
// if(!req.files[0] || !req.files[0].cloudStoragePublicUrl){
// return next(new HttpError('cloudStoragePublicUrl err.', 422))
// }
//images: req.files.cloudStoragePublicUrl,
console.log('HEllo World');
try{
await post.save();
}catch(err){
const error = new HttpError(
'Something went wrong, could not update post', 500
)
}
res.status(200).json({post: post.toObject({getters: true})})
}
您可以按如下所述安排代码:
const func1 = require(from file 1);
const func2 = require(from file 2);
async function load() {
const result1 = await func1();
const result2 = await func2()
}
这两个函数应该是异步的,现在创建一个文件,从两个文件中加载这两个函数,并使用wait。有关更多信息:您正在使用
async
/wait
。为什么?如何采用此方法来解决问题?等待函数实际停止执行,直到它完成当前执行。例如:wait func1()wait funct2()现在funct2()在func1完成执行之前不会开始执行