From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext
Unlock this course with a free trial
Join today to access over 25,000 courses taught by industry experts.
Use Promises to handle asynchronous operations - JavaScript Tutorial
From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext
Use Promises to handle asynchronous operations
- So now that we've seen how callbacks work, there is one pretty nasty drawback to using callbacks for asynchronous code. Since with callbacks, any results that you get from an asynchronous operation are only accessible inside the callback. This makes things a bit difficult when writing software where there are many asynchronous operations that take place one after the other. For example, our software could have to read some data out of a file, send that data to a server via a network request, update some data on the server with another request, and finally make one last request to get the current state of the data on the server. And all of this leads to what's become known as callback hell. That is a series of nested callbacks that slowly move over to the right, the deeper you get, and become unreadable very quickly. Now, one possible solution to this, and really the only solution until the async/await syntax was introduced in ES6, we'll get to that in a bit, was to use something…