Asynchronous Programming in Node.js, Promises, Generators, Async/Await

Implementing worker queues for processing datasets in Node.js

(Thu May 27 2021 00:00:00 GMT+0300 (Eastern European Summer Time)) Processing large datasets can be done with a simple for loop. But, processing one data item at a time, eliminates possible efficiency gains from processing data in parallel. But, parallel processing is not scalable, since it can easily swamp CPU or memory resources. Worker queue packages help by constraining the number of parallel tasks executing at any one time.

Node.js Script writers: Top-level async/await now available

(Thu Aug 13 2020 00:00:00 GMT+0300 (Eastern European Summer Time)) Async/await functions are a god-send to JavaScript programmers, simplifying writing asynchronous code. It turns difficult-to-write-and-debug pyramids of doom into clean clear code where the results and errors land in the natural place. It's been wonderful except for one thing, we could not use the await keyword in top-level code in Node.js. But, with Node.js 14.8 that's fixed.

Getting started with asynchronous read/write between Node.js 8 and MongoDB

(Wed May 23 2018 00:00:00 GMT+0300 (Eastern European Summer Time))

Node.js and MongoDB work very well together, by marrying a JavaScript programming language with a JavaScript-oriented database engine. In this article we'll look at usage patterns for asynchronous coding in Node.js using async functions against the MongoDB database. Async functions are a powerful new feature in JavaScript, and we want to be sure to use them as widely as possible. Async functions gives cleaner database code because results plop into a convenient place.

Avoid killing performance with asynchronous ES7 JavaScript async/await functions

(Sat May 12 2018 00:00:00 GMT+0300 (Eastern European Summer Time))

While async functions in JavaScript are a gift from heaven, application performance can suffer if they're used badly. The straightforward approach in using async functions in some cases makes performance go down the drain. Some smart recoding can correct the problem, unfortunately at the cost of code clarity.  

Streamline Mocha/SuperTest REST tests with async functions

(Tue Oct 17 2017 00:00:00 GMT+0300 (Eastern European Summer Time))

Mocha is an excellent unit testing framework for Node.js, and SuperTest is an excellent library for testing REST services. SuperTest is based on SuperAgent which makes it pretty darn easy to write code interfacing with a REST service. The problem with this combination is the test code can get pretty darn caught up in boilerplate callback functions, if you use Mocha and SuperTest the way its documented. Fortunately it's pretty easy to make SuperTest return a Promise, which then plays well with async functions, and Mocha is easily able to use an async function in a test scenario. So let's get started.

Asynchronous array operations in ES7 JavaScript async/await functions

(Wed Oct 26 2016 00:00:00 GMT+0300 (Eastern European Summer Time))

A glaring problem with the most excellent async/await feature in JavaScript is all the existing coding structures that only work with synchronous code. In an async/await function it's trivially easy to write a simple "for" loop that contains whatever async/await calls you want. But as soon as you try "Array.forEach" with an async callback function it will fail miserably. That's because this function, like so many existing interfaces which take a callback function, assumes the callback is synchronous. Instead of waiting for the Promise to resolve, it plows on ahead executing the next iteration.