(Post was originally written as a response to a question about how to resolve "callback hell" when asynchronous calls need to be executed sequentially.)
A popular system for managing sequential asynchronous operations, called "Promises", was integrated into ES6 (the latest release version of ECMAScript/Javascript). It's definitely a good idea to make sure you understand basic callbacks before diving into the Promises API, but it can really streamline asynchronous sequences once your callbacks start to get hellish.
It's based on an abstract data type called a "thenable". The basic premise is pretty simple -- a "promise" contains an operation that will be executed asynchronously, and has a .then()
method which can be used to specify an operation to follow once the promise is fulfilled. You can create a new promise like this:
// Promise.new() creates a new promise. It accepts a function as an argument.