Asynchronous programming with async/await pattern

JavaScript is single threaded

Everything running in JavaScript is running in the single OS process and consume a single thread. But when you are waiting for something outside the JavaScript process to happen, like waiting for database query to be executed or some file stream reader to load a file, you are able to return control back to your main JavaScript event loop and therefore any new event/request in your JavaScript program will be processed in the meantime.

It means your JavaScript program, if written properly, will never really waiting. It just runs all the time.

Async/Await pattern

Returning control to JavaScript event loop when waiting something outside JavaScript to happen,  is not automatic. You need to specify this manually in code with proper use of “Promise” objects and async/await functionality.

Sometimes the promise object is implicitly defined by “async” keyword in front of the function but on the end you always work with the promises.

Using asynchronous programming with TypeScript

To learn how to properly use promises and async/await in typescript I create a very small web application.

Create minimal server app

For this mini app I use Koa server. The app will support two routes: “/” and “/start”.  With “start” I will start long running operation and with “/” route I will check if server is still available and responsive.

Prepare project for sample code

 

After running the sample (index.ts) node application we should be able to start long running task with navigating to “localhost:3002/start” address and in the same time check if the same server is still active and responsive with default route “localhost:3002” which show new value of the counter after each refresh.

Long running task will not always succeed, only when random value is higher then 500 will. If value is lower then the error is thrown and implemented error handling intercept it with try/catch block.

Code for this sample app:

As we see the code looks more or less like ordinary synchronously written one except async keyword used in function declaration and await keyword where asynchronous methods are called.

Promise

If function is marked as async then the function will automatically return promise object. If function doesn’t have return statement, then this promise will be void (Promise<void>). If you write return with some real value, then promise will represent that value. Function will automatically wrap your value and return promise with it. Returning a value from async function means resolving the promise. If you throw exception from async function means your promise will be rejected.

Writing promise by hand

If you use async functions what’s really behind is actually exactly the same as when you work with promises directly, just syntactically is more synchronous looking and much more readable.

Working with multiple async functions in parallel

If you need to execute more then one function in parallel, you need to use Promise.all( [ array of promises] ).

The parallel call is made in the line :

Await in “parallel()” function will be called back after all promises will be resolved. The longest operation in array of promises define the maximum time spend here.

 

 

Leave a Reply

Your email address will not be published.