有了Promise,为什么浏览器会两次返回拒绝,但不会两次解决? [英] With a Promise, why do browsers return a reject twice but not a resolve twice?

查看:41
本文介绍了有了Promise,为什么浏览器会两次返回拒绝,但不会两次解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解javaScript promises 时遇到了麻烦.我写了以下代码:

I'm having trouble understanding javaScript promises. I wrote the following code:

var p = new Promise(function(resolve,reject){

    reject(Error("hello world"));
});

setTimeout(()=>p.catch(e=>console.log(e)),5000);

我立即在Chrome开发者控制台中看到以下内容:

I immediately see this in my Chrome developer console:

但是,我等待5秒钟后,该消息会自动变为黑色,如下图所示:

But after I wait 5 seconds, the message automatically changes to black like this image:

我以前从未见过我的javaScript代码和开发者控制台之间的这种行为,在那里,我的javaScript代码可以在开发者控制台中修改现有内容".

I've never seen this behaviour before between my javaScript code and a developer console, where my javaScript code can "modify existing content" in the developer console.

因此,我决定通过编写以下代码来查看 resolve 是否发生相同情况:

So I decided to see if the same situation occurs with resolve by writing this code:

var p = new Promise(function(resolve,reject){

    resolve("hello world");
});

setTimeout(()=>p.then(e=>console.log(e)),5000);

但是在这种情况下,我的开发者控制台要等5秒钟后才显示任何内容,然后在该控制台上显示 hello world .

But in this situation, my developer console doesn't show anything until 5 seconds later, to which it then prints hello world.

为什么调用 resolve reject 的时间如此不同?

Why are the resolve and reject treated so differently in terms of when they are invoked?

额外

我也写了这段代码:

var p = new Promise(function(resolve,reject){

    reject(Error("hello world"));
});

setTimeout(()=>p.catch(e=>console.log("errors",e)),5000);
setTimeout(()=>p.catch(e=>console.log("errors 2",e)),6000);
setTimeout(()=>p.catch(null),7000);

这会导致向开发人员控制台的多个输出.红色错误在时间0处出现,红色在5秒处变为黑色,并带有文本 errors hello world ,然后在6秒钟处出现新的错误消息,然后在7秒的时间出现红色错误消息.现在我对 reject 实际被调用多少次感到非常困惑....我迷路了...

This causes several outputs to the developer console. Red error at time 0, red changes to black at time 5 seconds with the text errors hello world, then a new error message at time 6 seconds errors 2 hello world, then a red error message at time 7 seconds. Now I'm very confused on how many times a reject actually gets invoked....I'm lost...

推荐答案

哇,这真的很酷.我以前从未见过控制台能做到这一点.(尽管它具有其他形式的动态行为,因此,...)这是正在发生的事情:

Wow, that's really cool. I'd never seen the console do that before. (It has other forms of dynamic behavior, though, so...) Here's what's going on:

在第一种情况下,您在 setTimeout 回调代码之外的所有内容的代码执行完成,并且执行堆栈返回,因此仅"未处理的拒绝,devtools会向您报告.

In the first case, code execution of everything outside your setTimeout callback's code completes and the execution stack returns so that only "platform code" (as the Promises/A+ spec calls it) is running, not userland JavaScript code (for the moment). At that point, the promise is rejected and nothing has handled the rejection, so it's an unhandled rejection and devtools reports it to you as such.

然后,五秒钟后,您的回调运行并附加了拒绝处理程序.在这一点上,拒绝不再是未处理的.显然,Chrome/V8/devtools可以一起从控制台删除未处理的拒绝警告.相反,您看到的是通过 console.log 在拒绝处理程序中输出的内容.如果您更快地附加了拒绝处理程序,则不会收到未处理的拒绝错误.

Then, five seconds later, your callback runs and attaches a rejection handler. At this point, the rejection is no longer unhandled. Apparently, Chrome/V8/devtools work together to remove the unhandled rejection warning from the console. What you see appear instead is what you output in your rejection handler via console.log. If you attached the rejection handler sooner, you wouldn't get that unhandled rejection error.

这不会在履行中发生,因为不处理履行不是错误条件.不处理拒绝.

This doesn't happen with fulfillment because not handling fulfillment isn't an error condition. Not handling rejection is.

这篇关于有了Promise,为什么浏览器会两次返回拒绝,但不会两次解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆