I am trying to read huge GeoJSON files in NodeJS. Due to the size, I am using JSONStream as fs.readFileSync couldn't handle it on my machine.
However, for some reason JSONStream does process my data (console.log inside on() works) but then the asynchronous function it is executed in does not seem to return the collected results. Instead, the program just ends. The commands after the function in question are not executed, but there are also no errors.
I reduced my TypeScript script to a minimal example that replicates the issue.
import fs from "fs";
import JSONStream from "jsonstream";
import { once } from "events";
import { Feature } from "geojson";
async function myFunction(jsonPath: string) {
const result: string[] = [];
const stream = fs.createReadStream(jsonPath, { encoding: "utf8" });
const parser = JSONStream.parse("features.*");
stream.pipe(parser);
console.log("before data");
parser.on("data", (feature: Feature) => {
result.push(feature.geometry.type);
//console.log(feature.geometry.type)
});
console.log("after data")
await once(stream, "finish");
console.log("after finish")
return result;
}
(async () => {
console.log("before my function")
const result = await myFunction(process.argv[2]);
console.log("after my function")
console.log(result.length)
})();
When I execute it after tsc via node ./test.js /path/to/file.geojson, I only get the following output:
before my function
before data
after data
None of the other "console.log"s are executed - neither in the function ("after finish") nor after the function call ("after my function"). If I remove the slashes from the console.log inside the on(), all the element geometry types are successfully printed.
But crucially, whatever I defined after the const result = await getPoisForArea(process.argv[2]); line is not executed. The script just ends without any error.
How can I work with the result returned after the asynchronous function call?