繁体   English   中英

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

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

我有一个使用koa.js的应用程序,出于上下文考虑,我正在与不严格遵循请求/响应模式的外部系统进行交互。 IE浏览器 在“请求”之后,它可能会回答也可能不会回答。

我可以将我的请求与这些响应进行匹配,但是随后我无法将其放入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 : ''
    };

});

所以我的问题是,如何在回调(或类似方法)中使用koa回答http请求,以及在未调用回调时(例如,可能经过延迟)如何发送空答案?

我猜我正在寻找类似于Promise.race()东西,但对于koa ,所以使用yield

最后,我能够使用bluebird's Promise.race()

我仍然会对使用生成器的解决方案感兴趣。

暂无
暂无

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

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