Search code examples
javascriptpromisees6-promise

How is Promise built?


I'd like to see the source code of the JavaScript Promise.I tried toSource() but it still just displayed ƒ Promise() { [native code] }. Is there any way I can see the native code?


Solution

  • Promises are built into the V8 Javascript interpreter and are implemented partly in native code in the internals of the interpreter. All the node.js code including the V8 JS engine is available on Github at https://github.com/nodejs/node and a good part of the V8 promise implementation is here in that repository.

    The built-in code is not simple to read. If you want a pure Javascript implementation example to learn from, then any of the promise implementations written entirely in Javascript are probably easier to learn from. For example, the Bluebird promise library is here: https://github.com/petkaantonov/bluebird and a promise polyfill is here: https://github.com/taylorhakes/promise-polyfill.