Javascript is a single-threaded language, this in certain situations can be very limiting because the process is stuck executing on one thread and can't fully utilize the CPU it is running on but thanks to concurrency, it's single-threaded nature is less of a problem.
But wait, what is concurrency !?
I am glad you asked (Even if you didn't ask just pretend you did and we will move on 😉)
Basics
Concurrency means two or more processes running together in one thread but not at the same time, a lot of us have come across concurrency in Node JS but might not have noticed it (Prime example = me 😅).
Example:
You can run this code!!
The code in the example above must be familiar to most of us, but did you know this is a prime example of concurrency?. We all agree that line 7 is executed before line 5 right, Well that is concurrency!, multiple separate processes running in the same thread by taking turns to execute code.
These are the steps taken during execution.
fs.writeFile calls an underlying function which acts as a proxy between JS and C++
The function calls C++ code which creates a process on the event loop that will handle the write operation
console.log('Writing "Hello World!!" into file.txt')
The process writes content to
file.txt
The process returns and our callback is executed
console.log('Wrote "Hello World!!" into file.txt')
This is great and all but there is one side effect to writing code with concurrent behavior and it is affectionately called the "Callback Hell"
Example:
Writing a file and then reading from it.
This gets exponentially worse the more you need to use data provided by such a function but the entire ordeal can be avoided when you use Promises.
Promises
Promises are javascript structures that "Promise" the resolution/failure of asynchronous code and help us handle their successes/failures in a syntactically synchronous manner.
Example:
The above code does not look that much better but with promises also come along the async/await keywords which will be extra helpful in cleaning up our code.
The await
keyword helps us retrieve data resolved by a promise as if it were directly returned from a synchronous function, but await
only works from within an asynchronous function and this is where the async
keyword comes in, it helps us define asynchronous functions where we can use await
.
Example:
Now that is clean asynchronous code!!
Taking it further
Now that we can create promises and await
them, we no longer need to use callbacks. Here are some general examples.
Note: The default libraries in Node JS don't have great support for promises so we will be using third-party libraries for the async examples
API calls
Using callbacks
Using promises
Spawn processes
Using callbacks
Using promises
Conclusion
Concurrency is a beautiful thing, especially in large scale applications where speed is a huge priority and I hope this post helped you learn a bit more about it and how best to apply it.
Thanks for reading!!!
Consider giving me a follow on Twitter and you can check out my previous post here
Top comments (6)
Interesting reading, worth mentioning we should always surround our await with try catch block for error handling (future version of node will crash the process in case of un handled promise rejection). Cheers
Nice post, i just want to say one thing.
In your promise example, you are nesting readPromise() in a callbackhell structure again, even if there is less lines of code.
Maybe you don't know, and that's the reason i write this, to share knowledge, if inside then() block you return a promise again, you can append another then to the chain.
Thanks!!, didn't notice
Hi! Great article!
I've been using NodeJS since v0.10.28 and I've gone from callbacks to using an async library to using Promises. I'm almost certainly going to try using async/await now but the little fearful and lazy dev in me doesn't want to believe you and he really doesn't want to change his coding style. Can you help me out here? Is there a legitimate reason why I should listen to him and maybe stick to Promise-then chains I am all to familiar with at the moment?
There is no specific reason to use async/await over promise chains, it's all about what you prefer 😁
Great post....