我有一个使用koa.js的应用程序,对于上下文,我正在连接一个外部系统,这个外部系统并不严格遵循请求/响应模式。即。在“请求”之后,它可能回答,也可能不回答。
我能够将我的请求与这些响应相匹配,但随后我无法将其放入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。
发布于 2016-01-05 08:24:55
最后,我能够使用bluebird's Promise.race()。
我仍然对使用发电机的解决方案感兴趣。
https://stackoverflow.com/questions/34589517
复制相似问题