What does the operation HostPromiseRejectionTracker do?
Asked Answered
G

2

0

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.

Glidewell answered 18/11, 2019 at 3:10 Comment(0)
M
3

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.

Mader answered 18/11, 2019 at 20:41 Comment(4)
Can I somehow observe the result of the HostPromiseRejectionTracker?Glidewell
I thought you'd read the spec? "An implementation of HostPromiseRejectionTracker must complete normally in all cases", and in both of the two usages the action is performed without the result being used (i.e. there is no result). No, you cannot observe anything on the promise object. Of course, you might be able to observe the effects of whatever the hook does, like the unhandledrejection event, depending on what the host has chosen to implement.Mader
I read what you quoted, of course. Although there is no result since NormalCompletion - I know. Can you give me some code of unhandledrejection event?Glidewell
@Glidewell What do you mean? Spec text, engine implementation, or usage examples? There are many on the web.Mader
E
0

If I'm not wrong, HostPromiseRejectionTracker is an abstract operation ( Abstract operation in JavaScript are those which are used to aid the specification of the semantics of JavaScript language).

For Eg. When JavaScript does Coercion, converting one value type to another; it happens at compile time for JavaScript. It aids the dynamic nature of JavaScript.

In the same way HostPromiseRejectionTracker works, It checks whether there is a handle operation for Promise rejection or in simple way, are we handling rejection by using reject operation or not.

If we are not defining reject operation then it will look for the next handler, whether it is invalidating the previous error notification (In our case, this will be then handler). If the notification is not invalidated, it will notify the developer about the error.

Embranchment answered 18/11, 2019 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.