在Angular 2中拒绝Promise时未处理的Promise拒绝 [英] Unhandled Promise rejection when rejecting Promise in Angular 2

查看:127
本文介绍了在Angular 2中拒绝Promise时未处理的Promise拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试实现自己的Promise,以便在Angular 2中使用.如果我答应了诺言,我会得到一个Error: Uncaught (in promise): nope(…),但只有在第一个Promise被拒绝时才会提供.

I am currently trying to implement my own Promise to be used inside Angular 2. If I reject the promise, I'll get an Error: Uncaught (in promise): nope(…), but only on the first Promise to be rejected.

它是Angular 2.0.0-rc.4,但是我在其他行为中也注意到了这一点.我的问题是,这是我对Promises的理解中的错误,还是应该报告给Angular项目的错误?

It's Angular 2.0.0-rc.4, but I noticed this in other behaviors. My question is, is this an error in my understanding of Promises, or is this a bug that should be reported to the Angular project?

示例代码:

import {Component} from '@angular/core';
import {bootstrap} from '@angular/platform-browser-dynamic'
@Component({
    template: "TestComponent"
})
class TestComponent {
}
bootstrap(TestComponent, []);

let p = new Promise((resolve, reject) => {
    console.log("create promise");
    reject("nope");
});
console.log("setting up");
p.then(r => console.log("then: " + r));
p.catch(e => console.log("reject: " + e));
console.log("setup done");

控制台(Google Chrome 51.0.2704.106,Linux 64位):

Console (Google Chrome 51.0.2704.106, Linux 64 bit):

create promise
setting up
setup done
reject: nope
Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
Unhandled Promise rejection: nope ; Zone: <root> ; Task: Promise.then ; Value: nope
Error: Uncaught (in promise): nope(…)

推荐答案

应该是

p
.then(r => console.log("then: " + r))
.catch(e => console.log("reject: " + e));

p.then(...)单独创建未处理的链,这困扰着Zone.js.如果您已经处理了Bluebird的未处理的拒绝",您可能已经知道规则了.

p.then(...) alone creates unhandled chain, this bothers Zone.js. If you have dealt with Bluebird's 'unhandled rejections', you may already know the rules.

这篇关于在Angular 2中拒绝Promise时未处理的Promise拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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