What does the operation HostPromiseRejectionTracker do?

和自甴很熟 提交于 2020-03-25 21:30:33

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/58907520/what-does-the-operation-hostpromiserejectiontracker-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!