I looked at HostPromiseRejectionTracker in the ECMAScript specification, but still did not understand what it was doing. It does not have specific steps of the algorithm, so it is not clear how this operation works in the current code.
One thing is clear that HostPromiseRejectionTracker is called when creating a new Promise when executing a function that calls the reject function. And the second time when "then" method is called for the first time, HostPromiseRejectionTracker is called only for the first time when "then" method is called.
For example, the first case occurs in such code
var promiseA = new Promise((resolve, reject) => {
setTimeout(() => reject(new Error("Text of Error")), 3000)
});
The second case occurs
var promiseA = new Promise((resolve, reject) => {
setTimeout(() => reject(new Error("Text of Error")), 3000)
});
promiseA.then();
But I don’t understand what HostPromiseRejectionTracker does exactly. Who understands what this operation is doing, explain its meaning, purpose and manifestation in the working ECMAScript code.
It's a hook where the implementation can do custom things in the promise life cycle. As the name suggests, it is used for tracking promise rejections (and the installation of rejection handlers by the then
method), and it is used to implement the mechanics of the unhandledrejection
events in browsers and node.js. If the host does not choose to implement any rejection tracking, it's simply a no-op and will do nothing.