簡體   English   中英

根據數據將數據從1個流傳輸到2個不同的流

[英]pipe data from 1 stream to 2 different streams depending on the data

我有1個流,其中包含前綴為2個不同前綴的數據。 讓我們說1和0.我希望所有以1為前綴的數據僅通過管道傳輸到流A和0到流B.

我想過只使用2個轉換,如果它有正確的前綴,只允許數據通過。 我只是想知道是否有更優雅的方式來做到這一點。

//an idea

//transformA is a transform that only pushes data that is prefixed with 1
//transformB is a transformt hat only pushes data that is prefixed with 2
incomingStream.pipe(transformA).pipe(destinationA)
incomingStream.pipe(transformB).pipe(destinationB)

for-awaitif可能的話?

for await (const chunk of incomingStream) {
    if(chunk.startsWith("1")) {
        destinationA.write(transformA(chunk));
        continue;
    }

    if(chunk.startsWith("2")) {
        destinationB.write(transformB(chunk));
        continue;
    }

    destinationElse.write(chunk);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM