简体   繁体   English

并行解决 Promise

[英]Resolve promises together in parallel

I am trying to resolve the array of promises together.我正在尝试一起解决一系列承诺。 Not sure how to do it.不知道该怎么做。 Let me share the pseudo code for it.让我分享一下它的伪代码。

async function sendNotification(user, notificationInfo) {
    const options = {
        method: 'POST',
        url: 'http://xx.xx.xx:3000/notification/send',
        headers:
    { 'Content-Type': 'application/json' },
        body:
    { notificationInfo, user },
        json: true,
    };
    console.log('sent');

     return rp(options);
}

I have wrapped the sendNotification method in another method which returns the promise of rp(request-promise) module.我已经将sendNotification方法包装在另一个方法中,该方法返回 rp(request-promise) 模块的 promise。

Next i am pushing this sendNotification method in array of promise, something like this接下来我在 promise 的数组中推送这个sendNotification方法,像这样

const notificationWorker = [];
for (const key3 in notificationObject) {
                          if(notificationObject[key3].users.length > 0) {
                            notificationWorker.push(sendNotification(notificationObject[key3].users, notificationObject[key3].payload));  // problem is notification are going as soon as i am pushing in notificationWorker array.
                          }
                }
    // task 1 - send all notifications
const result = await Promise.all(notificationWorker); // resolving all notification promises together
    // task 2 - update values in db , after sending all notifications
    const result2 = await Promise.all(updateWorker); // update some values in db

In above code, my problem is notifications are going as soon as i am pushing it in notificationWorker array.在上面的代码中,我的问题是,一旦我将它推送到notificationWorker数组中,通知就会发送。 I want all notifications to go together, when i run await Promise.all(notificationWorker)当我运行await Promise.all(notificationWorker)

Not sure, how to achieve what i am trying?不确定,如何实现我正在尝试的目标?

I understood the question partially, but then i feel this is difference between nodejs working concurrently and we trying to achieve parallelism, isn't that so.我部分理解了这个问题,但后来我觉得这是nodejs同时工作和我们试图实现并行性之间的区别,不是这样的。

Nodejs just switching between the tasks by, and not actually parallely doing it.Child Process might help you in that case. Nodejs 只是在任务之间切换,而不是实际上并行执行它。在这种情况下,子进程可能会对您有所帮助。

So for eg.所以例如。 if you go through a snippet如果你通过一个片段 go

 function done(i){ try{ return new Promise((resolve,reject)=>{ console.log(i); resolve("resolved " + i + "th promise"); }) }catch(e){ return null; } } let promises = []; for(let i=0;i < 100000; i++){ promises.push(done(i)); }

So console starts even when you dont call Promise.all right?因此,即使您不调用 Promise,控制台也会启动。好吗? this was your question but infact Promise.all should not suffice your thing, should go by spwaning child processes to achieve parallelism to some extent.这是您的问题,但实际上 Promise.all 应该不足以满足您的需求,如果 go 通过生成子进程以在一定程度上实现并行性。

The point i am trying to make it you are potraying the question to do something like first generate array of promises and start all of them once when Promise.all is called but in my opinion Promise.all also will be running concurrently not giving you what you want to achieve.我试图让你提出的问题是首先生成一系列承诺并在调用 Promise.all 时启动所有这些承诺,但在我看来 Promise.all 也将同时运行而不给你什么你想要达到的。

Something like this - https://htayyar.medium.com/multi-threading-in-javascript-with-paralleljs-10e1f7a1cf32 ||像这样的东西 - https://htayyar.medium.com/multi-threading-in-javascript-with-paralleljs-10e1f7a1cf32 || How to create threads in nodejs 如何在nodejs中创建线程

Though most of these cases show up when we need to do a cpu intensive task etc but here we can achieve something called map reduce to distribute you array of users in parts and start that array to loop and send notifications.虽然当我们需要执行 cpu 密集型任务等时,大多数情况都会出现,但在这里我们可以实现称为 map reduce 的东西,以将用户数组分部分分配并启动该数组以循环和发送通知。

All of the solutions, i am presenting is to achieve some kind of parallelism but i dont think sending array of huge amount of users would ever be done easily (with less resources - instance config etc) at same instant我提出的所有解决方案都是为了实现某种并行性,但我不认为在同一时刻发送大量用户数组会很容易(使用更少的资源 - 实例配置等)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM