【发布时间】:2016-01-04 10:54:24
【问题描述】:
我有一个使用 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。
【问题讨论】:
标签: javascript node.js koa co