I didn't realize how perilous such a simple task could be.
We're trying to stream-read a JSON file stored in S3--I think we have that part working.
Our .on('data') callback is getting called, but Node picks and chooses what bits it wants to run--seemingly at random.
We set up a stream reader.
stream.on('data', async x => {
await saveToDb(x); // This doesn't await. It processes saveToDb up until it awaits.
});
Sometimes the db call makes it to the db--but most of the time it doesn't. I've come to the conclusion that EventEmitter has problems with async/await event handlers. It appears as though it will play along with your async method so long as your code is synchronous. But, at the point you await, it randomly decides whether to actually follow through with doing it or not.
It streams the various chunks and we can console.log them out and see the data. But as soon as we try to fire off an await/async call, we stop seeing reliable messages.
I'm running this in AWS Lambda and I've been told that there are special considerations because apparently they halt processing in some cases?
I tried surrounding the await call in an IFFY, but that didn't work, either.
What am I missing? Is there no way of telling JavaScript--"Okay, I need you to run this async task synchronously. I mean it--don't go and fire off any more event notifications, either. Just sit here and wait."?