Dealing with asynchronous code—meaning any kind of code that doesn’t execute immediately—can be tricky. Asynchronous behavior is one of the main sources of complexity in any software environment. It represents a break in execution flow that may lead to any number of outcomes. This article introduces the three ways to handle asynchronous behavior in JavaScript. We’ll start with a look at callbacks, then I’ll introduce promises and the async and await keywords as more modern alternatives.
What is asynchronous code?
Asynchronous code (async code) says: go do something while I do other things, then let me know what happened when the results are ready. Also known as concurrency, async is important in a variety of use cases but is especially vital when interacting with external systems and the network. In the browser, making remote API calls is a ubiquitous use case for asynchronous code, but there are innumerable others on both the front- and back-end.
To read this article in full, please click here
InfoWorld