Created
September 8, 2022 04:21
-
-
Save gagan-bansal/20222a14ea0cfdf071ace7c008c24925 to your computer and use it in GitHub Desktop.
Process stream transform in parallel and output the data as soon as its processed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// source from 'parallel-transform' | |
var transform = require('parallel-transform'); | |
var stream = transform(5, {ordered: false}, function(data, callback) { // 10 is the parallism level | |
setTimeout(function() { | |
callback(null, data); | |
}, Math.random() * 1000); | |
}); | |
for (var i = 0; i < 20; i++) { | |
stream.write(''+i); | |
} | |
stream.end(); | |
stream.on('data', function(data) { | |
console.log(data); // prints 0,1,2,... | |
}); | |
stream.on('end', function() { | |
console.log('stream has ended'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment