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. 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 :
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) ?
I am guessing that I am looking for something similar to Promise.race()
, but for koa
, so using yield
.