0

I am trying to locate the source code of Promise in nodejs. But there are too many files. I can not find which file contains the implementation of Promise. Could anybody show me? Thanks.

https://github.com/nodejs/node

2
  • 1
    It’s all in src/js_native_api_v8.cc. Oct 21, 2019 at 3:49
  • @SebastianSimon - I don't see hardly any of the promise implementation there. There are two related functions, but they don't contain much of the implementation.
    – jfriend00
    Oct 21, 2019 at 4:22

2 Answers 2

3

Promises in nodejs come from V8 itself (the Javascript engine) and are not specific to just node.js. Because it comes from V8, it's the same implementation in node.js, Chrome, Opera, Edge, etc... who all use the V8 engine.

In the code, parts of the implementation are all over the place. Here are a bunch of files:

https://github.com/nodejs/node/blob/80f2b6736724e0a3b90731deb9503013edc7d85e/deps/v8/src/builtins/builtins-promise-gen.h

https://github.com/nodejs/node/blob/f53a2414474c2b1aee07ab367415b32c20e50bd9/deps/v8/src/runtime/runtime-promise.cc

https://github.com/nodejs/node/blob/80f2b6736724e0a3b90731deb9503013edc7d85e/deps/v8/src/builtins/builtins-promise-gen.cc

https://github.com/nodejs/node/blob/7cf6f9e964aa00772965391c23acda6d71972a9a/lib/internal/process/promises.js

https://github.com/nodejs/node/blob/f53a2414474c2b1aee07ab367415b32c20e50bd9/deps/v8/src/builtins/builtins-promise.h

https://github.com/nodejs/node/blob/80f2b6736724e0a3b90731deb9503013edc7d85e/deps/v8/src/objects/js-promise.h

As I find is often the case in node.js code, it's not simple to just go look at the whole promise implementation in one place, particularly with functions that use native code.

The implementation appears to be mostly native code which is probably because it's internal to V8, not specific to nodejs. Thus, Chrome, Edge, nodejs and any other programs that use V8 would have the same promise implementation. This makes sense since it's a standard part of the Javascript language now.

0
1

You can find an implementation of promise using nodejs internal/util module util.promisify. please check source below

https://github.com/nodejs/node/blob/master/lib/internal/util.js

1
  • 1
    util.promisify() just wraps an async callback function in a promise. It doesn't actually contain the promise implementation (which I think what the OP was asking for).
    – jfriend00
    Oct 21, 2019 at 4:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.