Skip to content

Instantly share code, notes, and snippets.

@Domiii
Created June 20, 2020 07:48
Show Gist options
  • Save Domiii/1eeedd50d911ee8a651a2452594443a5 to your computer and use it in GitHub Desktop.
Save Domiii/1eeedd50d911ee8a651a2452594443a5 to your computer and use it in GitHub Desktop.
JS in depth - odd behavior

When are chained callbacks in promises resolved?

Promise.then's "onFulfilledHandler" is called asynchronously. Meaning the callback will enter the queue for immediate execution after current stack fully unraveled. Calling 1000 then's takes a few milliseconds:

function f(x) {
    if (x > 0) {
        return Promise.resolve(--x).then(f);
    }
    console.timeEnd('f');
}

console.time('f');
f(1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment