I have a hapi.js route where I want to defer the response. I've tried storing the reply
function and calling it later, or wrapping it in a promise, but hapi always responds immediately with a 500 Internal Server Error response.
Store the reply for later:
var pendingReplies = {};
server.route({
method: "POST",
path: "/",
handler: function (request, reply) {
var id = generateId();
pendingReplies[id] = reply;
}
});
... // reply later by calling:
function sendResponse(id, data) {
pendingReplies[id](data);
}
I've tried creating a promise to reply:
handler: function (request, reply) {
var id = generateId();
return new Promise(function (resolve, reject) {
pendingReplies[id] = resolve;
}).then(function (data) {
return reply(data);
});
}
I've tried using reply().hold()
handler: function (request, reply) {
var id = generateId();
var response = reply().hold();
pendingReplies[id] = function (data) {
response.source = data;
response.send();
};
return response;
}
I've tried using reply().hold()
with a Promise:
handler: function (request, reply) {
var id = generateId();
var response = reply().hold();
return new Promise(function (resolve, reject) {
pendingReplies[id] = resolve;
}).then(function (data) {
response.source = data;
response.send();
return response;
});
}
With each of these, as soon as the handler function exits, I get a 500 response.
Is it possible with hapi to reply to a request asynchronously from outside of the route handler?
Aucun commentaire:
Enregistrer un commentaire