简体   繁体   English

按顺序触发异步调用

[英]Fire async calls in order

I've got some code right now that looks like this:我现在有一些看起来像这样的代码:

const a = await function1();
const b = await function2();
const c = await function3();

However I'm trying to optimise for speed and don't want the delays.但是我正在尝试优化速度并且不希望延迟。 I have tried doing the following我尝试过以下操作

[const a, const b, const c] = await Promise.all([function1, function2, function3)]

This works fine however the problem I then have is that I can't preserve the order of the calls initiated from my machine.这很好用,但是我遇到的问题是我无法保留从我的机器发起的调用的顺序。 As I send out a nonce on each request the end server will decline the request if it goes in order 1 -> 3 -> 2 instead of 1->2->3.当我在每个请求上发送一个随机数时,如果它按照 1 -> 3 -> 2 而不是 1->2->3 的顺序进行,最终服务器将拒绝该请求。

Is there a way to use the Promise.all which doesn't wait for each function to complete but still retain the order of the calls?有没有办法使用 Promise.all 不等待每个 function 完成但仍保留调用顺序?

If you do:如果你这样做:

const [a, b, c] = await Promise.all([function1(), function2(), function3()];

the synchronous part of function1 will run first, followed by the synchronous part of function2 , followed by the synchronous part of function3 . function1的同步部分将首先运行,然后是function2的同步部分,然后是function3的同步部分。 At that point, they're in a race for completion.那时,他们正在为完成而竞争。 Regardless of the order they complete in, you'll get the results in order ( a is the result of function1 , b of function2 , etc.).无论它们完成的顺序如何,您都会按顺序获得结果( afunction1的结果, bfunction2的结果,等等)。

If the server requires that the asynchronous part of function1 finish before function2 starts, then you have to use your original code.如果服务器要求function2异步部分在function1启动之前完成,那么您必须使用您的原始代码。 The only control you have in the client-side code is when the processes start .您在客户端代码中拥有的唯一控制是进程何时开始

However , that doesn't mean it's not possible to optimize them a bit, but it depends a lot on what they do.但是,这并不意味着不可能对它们进行一点优化,但这在很大程度上取决于它们的工作。 For instance, if the important thing is that you receive the headers sent in response to function1 's request before you start function2 , but function1 does further processing after receiving the headers (such as reading and doing something with the body of the response), you can optimize that but splitting function1 into two parts: The part that starts the process and consumes the headers, and the part that consumes the body and does something with it.例如,如果重要的是您在启动function2之前收到了响应function1的请求而发送的标,但function1在收到标头后会进行进一步处理(例如读取响应的正文并对其进行处理),您可以对其进行优化,但将function1分为两部分:启动进程并使用标头的部分,以及使用主体并对其进行处理的部分。

If you did that, you'd do it like this:如果你这样做了,你会这样做:

// Do the first part in series
const r1 = await function1PartA();
const r2 = await function2PartA();
const r3 = await function3PartA();
// Do the remainder in parallel
const [a, b, c] = await Promise.all([
    function1PartB(r1),
    function2PartB(r2),
    function3PartB(r3)
]);

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

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