简体   繁体   English

Koa.js在竞争条件下产生值

[英]Koa.js yield value with a race condition

I have an app that uses koa.js , and for context I am in the process of interfacing an external system that doesn't strictly follow request/responses pattern. 我有一个使用koa.js的应用程序,出于上下文考虑,我正在与不严格遵循请求/响应模式的外部系统进行交互。 IE. IE浏览器 after a "request", it may or may not answer. 在“请求”之后,它可能会回答也可能不会回答。

I am able to match my requests to these responses, but then I am unable to put that into the koa.js response : 我可以将我的请求与这些响应进行匹配,但是随后我无法将其放入koa.js响应中:

r.get('/...', *function() {

    // (1) cannot yield since it will block and never call (2) ?
    callbacks.storeCb(howToMatchAnEventualResponse, function(err, resp) {  // may never get called depending about how the external system answers
        console.log("I should answer the http req now"); // how to answer the request from here ?
    });

    // has to be done after storingCb, this may or may not trigger the callback above
    externalSystem.sendMessage(msg); // (2)

    // something similar will have to be done in the callback instead
    this.body = {
        success : true,
        response : ''
    };

});

So my question is, how do I answer the http request using koa in my callback (or something similar), and how can I send an empty answer when the callback is not called (ie. after a delay maybe) ? 所以我的问题是,如何在回调(或类似方法)中使用koa回答http请求,以及在未调用回调时(例如,可能经过延迟)如何发送空答案?

I am guessing that I am looking for something similar to Promise.race() , but for koa , so using yield . 我猜我正在寻找类似于Promise.race()东西,但对于koa ,所以使用yield

Well in the end I was able to use bluebird's Promise.race() . 最后,我能够使用bluebird's Promise.race()

I would still be interested into a solution using generators. 我仍然会对使用生成器的解决方案感兴趣。

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

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