简体   繁体   English

两个不同文件中的两个异步代码。 我希望它们一个接一个地运行,但它们同时运行

[英]Two asynchronous codes in two different files. I want them to run one by one but they run at the same time

The code in the second file depends upon the first one so I want the first file code to execute first and when the first file code completes it's execution then the code in second file should run.第二个文件中的代码取决于第一个文件,所以我希望第一个文件代码首先执行,当第一个文件代码完成它的执行时,第二个文件中的代码应该运行。 The code in the first file basically stores the image in the google cloud bucket and then return the URL of that image and second file updates the post schema in MongoDB and pushes the URL string in the images array.第一个文件中的代码基本上将图像存储在谷歌云存储桶中,然后返回该图像的 URL,第二个文件更新 MongoDB 中的帖子模式并将 ZE6B391A8D2C4D45902A23A8B658 中的字符串推送到图像数组中。 Since the code in both files runs side by side and the code in the second files does not wait for first one to execute the post schema is updated even before the first code returns all the string url's which is a problem.由于两个文件中的代码并排运行,并且第二个文件中的代码不会等待第一个文件执行后模式,因此即使在第一个代码返回所有字符串 url 之前,它也会更新,这是一个问题。 I want to get all the string url's first and then update the post schema.我想先获取所有字符串 url,然后更新帖子架构。

So in first file I have所以在第一个文件中我有

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);
})

}

In the second file I have在我的第二个文件中

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})})
}

You may arrange your code like mentioned below:您可以安排您的代码,如下所述:

const func1 = require(from file 1);
const func2 = require(from file 2);
async function load() {
    const result1 = await func1();
    const result2 = await func2()
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AJAX - 同时运行两个请求,但其中一个请求重复一段时间 - AJAX - run two requests at same time, but one of them repeatedly with some interval 使用 angular 我想通过只输入一个文件来输入两个名称相同但文件扩展名不同的文件 - Using angular I want to input two files with same name but different file extension by doing the input of just one Javascript - 我有两个事件侦听器,它们运行相同的全局 scope function,但其中一个不工作……为什么? - Javascript - I have two event listeners which run the same global scope function, but one of them isn't working…why? 我有两个约会。 我想通过时间差设置另一个与第一个相同的 - I have two dates. i want to set another one same as first one by taking time difference 两个单独的函数,但是我希望它们同时被初始化 - Two separate Functions, but I want them to be initialized at the same time 我有两个具有相同代码的不同域,但是其中一个不起作用。 为什么? - I have two different domains with the same code, but one of them doesn't work. Why? 我想在一台服务器上运行两个nuxt.js(vue.js)应用程序。 (下午2点) - I want to run two nuxt.js(vue.js) apps on one server. (pm2) 两个递归函数尝试在只有一个应该运行的同时执行 - Two recursive functions try to execute at the same time when only one should run 使 addEventlistener 仅在具有不同对象功能的两个按钮之间运行一次 - make addEventlistener only run one time between two buttons with different object functions 如何同时运行两个具有不同功能的案例? - How to run two cases with different functions at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM