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.
2 Answers
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:
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.
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
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). Oct 21, 2019 at 4:20
src/js_native_api_v8.cc
.