简体   繁体   English

解决未决的并行承诺

[英]Resolving pending parallel promises

I have recently upgraded a HAPI installation to v17, which uses promises, and am facing a situation I do not know how to resolve. 我最近将HAPI安装升级到了使用Promise的v17,并且面临着一种我不知道如何解决的情况。 This is not HAPI-specific... 这不是特定于HAPI的...

When a certain route is called, a check is made to see if the results of a particular service call have been cached. 当调用某个路由时,将进行检查以查看特定服务调用的结果是否已被缓存。 If so, we skip the call and use the cached results - otherwise, a service call is made, which involves a promise. 如果是这样,我们将跳过该呼叫并使用缓存的结果-否则,将进行涉及承诺的服务呼叫。

The problem is, if the server is started up and the results have not been cached, and we run a load test, suddenly there's a flood of calls to the service to get the results, because they haven't been cached, yet, and a bunch of requests just came in at the same time. 问题是,如果服务器已启动并且尚未缓存结果,并且我们运行了负载测试,则突然会有大量的服务调用以获取结果,因为它们尚未被缓存,并且一堆请求是同时发出的。

What I want to do is only let the first request fire the service call, and just let all subsequent requests wait (via a promise?) for that call to return - then all pending promises can be resolved to let the other requests continue. 我只想让第一个请求触发服务调用,然后让所有后续请求等待(通过Promise?)等待该调用返回-然后可以解析所有待处理的Promise以使其他请求继续。

How do I structure this? 我该如何构造? In the past, using callbacks, I would just add the callbacks to a list, then call all the callbacks in that list once the results were in. Can I somehow do something similar, but stacking up promises until the call comes back, then resolve them all? 过去,使用回调,我只会将回调添加到列表中,然后在结果输入后调用该列表中的所有回调。我能以某种方式做些类似的事情,但是将promise堆积起来直到调用返回,然后再解决商场? I'm still new to promises, so having trouble visualizing it. 我对诺言仍然陌生,因此很难形象化它。

The basic simplified logic in place, currently, is: 当前,基本的简化逻辑是:

function getData() {
    return new Promise(...);
}

...

if (!data) {
    data = await getData();
}
return data;

Rather than just calling getData() if data is not cached, I want to set a flag when getData sends a request to get it, and have subsequent calls wait for that request to complete to pick up data and move on, rather than sending yet another request for it. 我不想在没有缓存数据的情况下仅调用getData(),而是要在getData发送请求以获取它时设置一个标志,并让后续调用等待该请求完成以拾取数据并继续前进,而不是发送另一个要求。

Thanks! 谢谢!

You may cache promise itself instead of caching data + flag-to-see-if-data-retrieved. 您可以缓存Promise本身,而不是缓存数据+获取数据的标志。

function loadData() {
    someOuterVariable = fetch(...).then(/*transforming data structure*/)
}

async function dataConsumer() {
    const data = await someOuterVariable; // does not matter if it has been retrieved or not
    .... 
}

async function anotherConsumer() {
    ....
    const item = (await someOuterVariable).filter(/* just an example of inline processing*/)
}

And it does not matter if data has already been retrieved or not - Promise is still Promise. 而且,无论是否已检索数据都没有关系-Promise仍然是Promise。

The only limitation is - you can update that someOuterVariable with new Promise but if any consumer function has already started to await - it will get old data from previous Promise. 唯一的限制是-您可以使用新的Promise更新someOuterVariable ,但是如果任何使用者功能已经开始await -它将从先前的Promise获取旧数据。 But according to your description it's not your case to substitute promise until it has been fulfilled, is it? 但是根据您的描述,在兑现诺言之前,不是您的情况下替代诺言吗?

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

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