更新:澄清和修改代碼,以反映我真的想要的東西,也就是發送流媒體響應,也就是發回匹配的結果,因爲它們到達從他們自己的async
匹配過程中。發送與異步操作流響應REST查詢
考慮(使用expressjs
-ish代碼)
app.post('/', jsonParser, function (req, res) {
if (!req.body) return res.sendStatus(400)
// matches is not needed for this scenario so
// commenting it out
// var matches = [];
req.body.forEach(function(element, index) {
foo.match(
element,
function callback(error, result) {
if (error) {
console.log(error); // on error
}
else {
⇒ if (some condition) {
// matches.push(result);
⇒ res.send(result);
}
}
}
);
});
// moved this above, inside the callback
// ⇒ res.send(matches);
});
輸入到post('/')
是術語的陣列。每個術語使用foo
進行匹配,每次調用後都有callback
。我想發回所有符合「某些條件」的比賽(參見上述代碼中的⇒)。理想情況下,最好發送流式響應,即在匹配發生時發回響應(因爲foo.match()
可能需要一段時間)。我如何去做這件事?
你的問題是關於流或如何創建一個數組,最終會發回給客戶端? –
已更新的問題,以澄清我想發回流回應,而不是最終陣列。感謝您注意到混淆。 – punkish