微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Promise

Promise状态
pending resolved rejected
调resolve函数会进入成功状态
调rejected函数或报错会进入失败状态
api
Promise.all Promise.race Promise.resolve Promise.reject是函数对象方法
.then .catch是实例对象方法
.then返回值

  1. 返回非promise的值,则下一个promise变为resolved值为.then返回值
  2. 抛出异常,则下一个promise变为rejected,值为抛出的异常
  3. 返回promise的值,此promise结果为新promise结果
                Promise.resolve(2)
                .then(x => {
                    console.log(x); // 输出2,也就是上面resolve参数值
                    return 'hello'; // 回调函数返回字符串类型
                })
                .then(x => {
                    console.log(x); // 输出hello,也就是上一个then回调函数返回值,表明上一个then的返回值就是下一个then的参数
                    // then函数回调函数中没有返回值
                }) 
                .then(x => {
                    // 前面的then的回调函数没有返回值所以这个x是undefined
                    console.log(x); // undefined
                }) 
                .then(() => {
                    return Promise.resolve('hello world');
                })
                .then(x => {
                    console.log(x); // hello world
                }); 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐