Promise 是什么?

Promise对象代表一个异步操作,有三种状态:Pending(准备状态),Fulfilled(成功状态,也称为Resolved状态),Rejected(失败状态)。只有异步操作的结果可以决定promise对象的状态,操作成功后,promise对象由Pending状态转换为Fulfilled状态,此时回调函数会执行 onFulfilled方法。反之,操作失败,promise对象由pending状态转换为Rejected状态,此时回调函数会执行onRejected方法。
如下图所示:


Promise状态改变
创建一个Promise对象
var p1 = new Promise(function(resolve,reject){
    resolve('resolve');
});
var p2 = new Promise(function(resolve,reject){
    reject('reject');
});

p1 返回一个resolved状态,值为resolve的Promise对象

Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: "resolve"} 

p2 返回一个rejected状态,值为reject的Promise对象

Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: "reject"}
Promise 的基本API
1.Promise#then

Promise.then(onFulfilled,onRejected)

then方法定义了promise状态变为resolved或者rejected状态之后的回调函数:

  • pending->resolved:执行onFulfilled函数,并且将promise对象的值传给onFulfilled函数
  • pending->rejected:执行onRejected函数,并且将promise对象的值传给onRejected函数

代码示例如下:

var p = new Promise(function(resolve, reject){
  console.log("create a promise");
  resolve("success");
});

console.log("after new Promise");

p.then(function onFulfilled(value){
  console.log(value);
},function onRejected(error){
  console.log(error);
});

运行结果如下:

"create a promise"
"after new Promise"
"success"
2.Promise#catch

new Promise返回一个resolved状态的promise对象,所以在p.then()方法中调用的是onFulfilled函数。
创建promise时,promise对象中的函数是立即执行的,并不是在调用then方法的时候才会去执行,所以首先会输出"create a promise"。但是执行的代码是异步代码,所以"after new Promise"会在"success"之前输出。

2.Promise#catch

promise.catch(onRejected);

catch方法捕获了promise运行过程中的异常。catch与then方法中的onRejected的不同是,onRejected 函数只能在promise状态变为rejected的时候调用,但catch既可以在promise状态变为rejected的时候调用,又可以捕获第一个onFulfilled方法中出现的错误。
可以参考promise迷你书中的这一段代码:

function throwError(value) {
    // 抛出异常
    throw new Error(value);
}
// <1> onRejected不会被调用
function badMain(onRejected) {
    return Promise.resolve(42).then(throwError, onRejected);
}
// <2> 有异常发生时onRejected会被调用
function goodMain(onRejected) {
    return Promise.resolve(42).then(throwError).catch(onRejected);
}
// 运行示例
badMain(function(){
    console.log("BAD");
});
goodMain(function(){
    console.log("GOOD");
});

在这段代码中,badMain函数中传入onRejected参数,因为Promise.resolve(42)返回一个resolved状态的promise对象(这个api的用法会在下一节中说到),所以会去调用throwError函数,throwError函数中抛出的异常onRejected是捕获不到的,但是catch可以捕获到。


catch异常捕获
3.Promise.resolve

这个方法会根据传进来的参数的不同,返回不同的promise对象。

//1.传入的参数是一个普通值
var p1 = Promise.resolve(1);//返回一个将该对象作为值的新promise对象
//2.传入的参数是一个Promise对象
var p2 = Promise.resolve(p1);//返回接收到的promise对象

p1 === p2   //true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值