Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 具有竞争条件的KOA.JS屈服值_Javascript_Node.js_Koa_Co - Fatal编程技术网

Javascript 具有竞争条件的KOA.JS屈服值

Javascript 具有竞争条件的KOA.JS屈服值,javascript,node.js,koa,co,Javascript,Node.js,Koa,Co,我有一个使用koa.js的应用程序,就上下文而言,我正在与一个不严格遵循请求/响应模式的外部系统进行接口。即,在“请求”之后,它可能会回答,也可能不会回答 我能够将我的请求与这些响应相匹配,但我不能把它放入Koajs响应: r.get('/...', *function() { // (1) cannot yield since it will block and never call (2) ? callbacks.storeCb(howToMatchAnEventualRe

我有一个使用
koa.js
的应用程序,就上下文而言,我正在与一个不严格遵循请求/响应模式的外部系统进行接口。即,在“请求”之后,它可能会回答,也可能不会回答

<>我能够将我的请求与这些响应相匹配,但我不能把它放入Koajs响应:

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 : ''
    };

});
<> P> >我的问题是,如何在回调中(或类似的)使用膝关节炎来回答HTTP请求,当调用回调时,如何才能发送空的答案(例如,在延迟之后)?
我猜我正在寻找类似于
Promise.race()
,但针对
koa
,所以使用
yield
最终我能够使用
蓝鸟的
Promise.race()


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

Koa中的生成器抽象主要用于简化路由逻辑(即处理请求),而不是用来解决所有问题(即不能)的抽象。“Koa方法”实际上是用返回承诺的函数包装更复杂的异步逻辑,然后在路由中生成它们(在您的情况下是
promise.race()
)。这样,您可以将复杂性排除在路由之外,但也可以编写简单的基于回调的异步代码,而这正是解决问题所需要的。将生成器视为路由处理程序使用的主要工具。两件事:1)
Promise.race
在ES2015和Bluebird中都有。2)Bluebird建议使用
Bluebird Promise.any
而不是
Bluebird Promise.race
,因为前者甚至将拒绝的承诺标记为“赢家”