[英]highland.js async array to stream of values
我正在嘗試獲取以下代碼片段以返回相同的輸出-數組值流。
第一種方法從數組開始並發出值。
第二種方法將解析數組的promise作為輸入,因此,它只發出數組本身,而不是發出每個值。
我應該在第二種方法中進行哪些更改,以使其輸出與第一種方法相同的東西?
const h = require('highland');
var getAsync = function () {
return new Promise((resolve, reject) => {
resolve([1,2,3,4,5]);
});
}
h([1,2,3,4,5])
.each(console.log)
.tap(x => console.log('>>', x))
.done();
//outputs 5 values, 1,2,3,4,5
h(getAsync())
.tap(x => console.log('>>', x))
.done();
//outputs >>[1,2,3,4,5]
在這兩種情況下,您都不需要調用done
,因為each
都已經占用了您的流。
有諾言的情況將解析的值(即數字數組)向下傳遞。 您可以使用series
方法將此流中的每個數組轉換為它自己的流,然后將這些流連接起來。 在此示例中,這有點違反直覺,因為只有一個數組,因此只有一個流可以串聯。 但這就是您想要的-一堆數字。
這是代碼:
const h = require('highland');
var getAsync = function () {
return new Promise((resolve, reject) => {
resolve([1,2,3,4,5]);
});
}
h([1,2,3,4,5]) // stream of five numbers
.each(console.log) // consumption
h(getAsync()) // stream of one array
.series() // stream of five numbers
.each(x => console.log('>>', x)) // consumption
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.