ECMAScript 6 promises (1/2): foundations
This blog post is outdated. Please read chapter “ Asynchronous programming (background) ” in “Exploring ES6”. This blog post explains foundations of asynchronous programming in JavaScript. It is first in a series of two posts and prepares you for part two , which covers promises and the ECMAScript 6 promise API. The JavaScript call stack When a function f calls a function g , g needs to know where to return to (inside f ) after it is done. This information is usually managed with a stack, the call stack . Let’s look at an example. function h(z) { // Print stack trace console.log(new Error().stack); // (A) } function g(y) { h(y + 1); // (B) } function f(x) { g(x + 1); // (C) } f(3); // (D) return; // (E) Initially, when the program above is started, the call stack is empty. After the function call f(3) in line (D), the stack has one entry: Location in global scope After the function call g(x + 1) in line (C), the stack...