2017-02-20 58 views

回答

3

簡短的回答是:是的,轉換流保證大塊以相同的順序發送。 (因爲流可能被用於訂單sensative操作(加密,壓縮和解或-解壓文件)

下面是一個剪斷,你可以運行,以確保:

const {Transform} = require('stream'); 
 
const _ = require('lodash'); 
 
const h = require('highland'); 
 

 
const myTransform = new Transform({ 
 
    transform(chunk, encoding, callback) { 
 
     //Callback fires in a random amount of time 1-500 ms 
 
     setTimeout(() => callback(null, chunk), _.random(1, 500)); 
 
    }, 
 
    //Using objectMode to pass-trough Numbers, not strings/buffers 
 
    objectMode: true 
 
}); 
 

 
//I'm using 'highland' here to create a read stream 
 
//The read stream emits numbers from 1 to 100 
 
h(_.range(1, 100)) 
 
    .pipe(myTransform) 
 
    //Simply logging them as they go out of transform stream 
 
    .on('data', chunk => console.log(chunk.toString())); 
 

 
//The output is: 
 
// 1 
 
// 2 
 
// 3 
 
// 4 ... 
 
//Although the callbacks fire in random order

相關問題