I am using KOA2 to demo a bigpipe. I want to output a result as soon as possible when it's ready, rather than waiting until all results are ready(then output all).
I stuck here as code below: (get_users
or get_posts
return a promise)
async (ctx, next) => {
ctx.res.write("<html><body>...")
let data = await Promise.all( [res_fetch.get_users(), res_fetch.get_posts()] )
ctx.res.write(data[0])
ctx.res.write(data[1])
ctx.res.write("</body></html>")
ctx.res.end()
}
Ideally it should be something: (which I don't know how to achieve)
let data = await Promise.oneFinished( [res_fetch.get_users(), res_fetch.get_posts()] )
if (data[0] is finished)
ctx.res.write(data[0])
else if (data[1] is finished)
ctx.res.write(data[1])
let data = await
Promise.all([
res_fetch.get_users().then(result => {
ctx.res.write(result)
}),
res_fetch.get_posts().then(result => {
ctx.res.write(result)
}),
])
ctx.res.write("</body></html>")
ctx.res.end()