Debunking Common Myths With Promises
We all have our unique mental pictures of how we visualize code running. With regards to JavaScript, you might be familiar with the constraint that JavaScript runtime is strictly single-threaded. Using Promises, we seem to overcome some bottlenecks of single-threaded applications. This is usually where misconceptions start to develop, especially that promises enable multi-threaded programming. Let’s bust some misconceptions!
Myth 1: Promises Enable Multi-Threaded JavaScript
JavaScript runtime is strictly single-threaded, but you have to remember that the JavaScript runtime is only one system (or “Thread”) in a browser or Node.js. Browsers and Node.js have other threads running in parallel to perform well-defined tasks that we can utilize in our JavaScript runtime.
For our practical use-cases, we can understand JavaScript as Single-threaded+ because on top of the dedicated JavaScript runtime thread, we can delegate tasks to other threads through:
- WebWorkers: Browser interface for creating separate threads to crunch some code. It is important to note that you cannot manipulate the DOM in a WebWorker thread…