简体   繁体   English

如何执行多个异步功能(非嵌套)以提高性能,但要等它们完成后再继续

[英]How can I do multiple async functions (not nested) for performance, but wait for them to finish before proceeding

I've been using Golang for quite some time, but I love writing Javascript so I've switched back, but in Golang you could use sync.WaitGroup to perform multiple goroutines and wait for them to finish, eg: 我已经使用Golang相当一段时间了,但是我喜欢编写Javascript,所以我切换回去,但是在Golang您可以使用sync.WaitGroup来执行多个goroutines并等待它们完成,例如:

var wg sync.WaitGroup
for _, val := range slice {
   wg.Add(1)
   go func(val whateverType) {
       // do something
       wg.Done()
   }(val)
}

wg.Wait() // Will wait here until all `goroutines` are done 
          // (which are equivalent to async callbacks I guess in `golang`)

So how can I accomplish something like this in Javascript (Node). 因此,如何在Javascript(节点)中完成类似的操作。 This is what I'm currently working with: 这是我目前正在使用的:

router.get('/', function(req, res, next) {

   // Don't want to nest
   models.NewsPost.findAll()
   .then(function(newsPosts) {
       console.log(newsPosts);
   })
   .error(function(err) {
      console.error(err);
   });

   // Don't want to nest
   models.ForumSectionPost.findAll({
       include: [
           {model: models.User, include: [
               {model: models.Character, as: 'Characters'}
           ]}
       ]
   })
   .then(function(forumPosts) {
       console.log(forumPosts);
   })
   .error(function(err) {
       console.error(err);
   });

    // Wait here some how until all async callbacks are done
  res.render('index', { title: 'Express' });
});

I don't want to nest each .findAll() into a return for the promise because then they will process in order and cost performance. 我不想将每个.findAll()嵌套到诺言的回报中,因为这样它们将按顺序和成本效益进行处理。 I'd like them to run together and then wait until all async callbacks are done and then proceed. 我希望它们一起运行,然后等到所有异步回调完成后再继续。

You'll need to use a library that supports Promise.all : 您需要使用支持Promise.all的库:

router.get('/', function(req, res, next) {

   // Don't want to nest
   var p1 = models.NewsPost.findAll()
   .then(function(newsPosts) {
       console.log(newsPosts);
   })
   .error(function(err) {
      console.error(err);
   });

   // Don't want to nest
   var p2 = models.ForumSectionPost.findAll({
       include: [
           {model: models.User, include: [
               {model: models.Character, as: 'Characters'}
           ]}
       ]
   })
   .then(function(forumPosts) {
       console.log(forumPosts);
   })
   .error(function(err) {
       console.error(err);
   });

  Promise.all([p1, p2]).then(function(){
     // Wait here some how until all async callbacks are done
     res.render('index', { title: 'Express' });
  });
});

暂无
暂无

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

相关问题 我如何在继续执行之前等待Javascript同步循环中的异步回调完成 - How can I wait for asynchronous callback inside Javascript synchronous loop to finish before proceeding 如何在继续之前等待 getDownloadURL 完成? - How to wait for getDownloadURL to finish before proceeding? 如何在继续之前等待graphQL中的查询完成? - How to wait for query in graphQL to finish before proceeding? 如何在JS(节点)中使用回调来等待Async函数完成才能继续? - How to use callback in JS (node) to wait for Async function to finish before proceeding? NodeJS:如何在继续执行代码之前等待异步请求的 for 循环完成? - NodeJS: How to wait for a for-loop of async requests to finish before proceeding with code? 如何让 IPFS 在继续之前添加等待等待? - How do I make the IPFS add await wait before proceeding? 如何在node.js中等待N个异步函数完成,以便对它们的所有组合结果进行一些工作? - How to wait for N number of async functions to finish in node.js so that I can do some work on all their combined results? 如何在 Angular 2 中继续之前等待可观察方法完成 - How to wait for a observable method to finish before proceeding in Angular 2 如何使 for 循环等待所有异步函数完成。 异步函数里面有多个异步函数 - How to make for loop wait for all async functions to finish. async functions has multiple async functions inside in it 等待异步功能完成,然后再继续 - Wait for asynchronous function to finish before proceeding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM