Concurrent Programming in JS
- JavaScript is single threaded, but web developers still have to deal with concurrency - like coordinating several simultaneous asynchronous connections to external servers, or mouse/keyboard events.
doAsyncOperationA(function(result1) {
doAsyncOperationB(result1, function(result2) {
doAsyncOperationC(result2, function(result3) {
handleResult(result3);
})
})
});
- Works ok(-ish) for simple chains, which can be sequentialized (kind-of).
... but anything more complicated quickly leads to "asynchronous Spaghetti".
"It is more difficult to develop applications using asynchronous mechanisms due to the separation in time and space between operation initiation and completion" (
boost::asio docs)