3

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?

Jaeeun Lee
  • 3,056
  • 11
  • 40
  • 60
  • https://github.com/taylorhakes/promise-polyfill – Piterden Oct 24 '18 at 02:09
  • Reopening because that duplicate was not very specific about promise code and did not have any references to how to see promise implementations written in Javascript. Just giving someone a top level link to the github repository is not as useful an answer as one that actually directly links the OP to promise-specific code. – jfriend00 Oct 24 '18 at 02:49
  • 1
    Basically a duplicate of [Read JavaScript native code](https://stackoverflow.com/q/9103336/476)… – deceze Oct 24 '18 at 02:59

1 Answers1

4

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • there are many promise implementations in javascript [here](https://promisesaplus.com/implementations) many of them very much more readable than bluebird or the promise polyfill – Bravo Oct 24 '18 at 07:45