I hope I'm asking the question correctly, but essentially, is there a way in NodeJS to have a CommonJS module return a stream of data instead of a finalized chunk of data after a (long) computation? Assuming there is more than one way, what are those techniques?
For example, say I have a findPrimes
function, written as a CommonJS module:
find-primes.js
/**
* @module findPrimes
* @param {int} n - Find all primes less than this number
* @return {array}
*/
module.exports = function(n) {
if (n < 2) {
return [];
} else if (n === 2) {
return [2];
}
var primes = [2];
for (let i = 3; i < n; i += 2) {
let is_prime = true;
let sq = Math.ceil(Math.sqrt(i));
for (let t = 2; t <= sq; t++) {
if (i % t === 0) {
is_prime = false;
break;
}
}
if (is_prime) {
primes.push(i);
}
}
return primes;
};
As you can see, this function returns an array of all the prime numbers less than its input. It returns the array after it has computed all those numbers.
So, say I go to use this module in a node script
index.js
const primes = require('./find-primes.js');
// Usage: `node index.js <num>`
let primes_less_than = process.argv[2];
console.log(primes(primes_less_than));
When I run the above script with an arg of 25
, I get the following (expected) output:
$ node index.js 25
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ]
However, say I passed in a much larger number, like 10,000,000
$ node index.js 10000000
# Takes a while to run before outputting the numbers...
While this works, ideally I'd like the program to start writing out the numbers it has computed before it finishes.
So my program would still take a while to run, but it would start outputting information to the screen much faster than "compute EVERYTHING first, then output ALL results".
What is the best way to achieve this effect? Streams? Promises?
I'm open to any and all techniques for this, thanks.
via romellem
No comments:
Post a Comment