Promise 笔记

1. Promise 创建

Promise 对象是用于处理延时和异步调用场景,ES6中已经定稿,这个是未来的趋势

初始化对象

    new Promise(function(resolve, reject) {
        resolve('success');
    }).then(function(arg){
        console.log(arg);//success
    }).then(function(arg){
        console.log(arg);//success
    });

对于Promise对象的链式处理方式

    p.then(onFulfilled, onRejected);
    p.then(function(value) {
    
    }, function(reason) {
    
    });
2. 异常处理

catch 和 Promise.prototype.then(undefined, onRejected) 是等同的,当需要注意的是其中catch和then的onRejected方法只能捕获一次异常

    var p1 = new Promise(function(resolve, reject) {
        resolve('Success');
    });

    p1.then(function(value) {
        console.log(value); // "Success!"
        throw 'oh, no!'; // 抛出一个异常
    }).catch(function(e) {
        console.log(e); // "oh, no!" 异常被捕获
    }).then(function() {
        console.log('after a catch the chain is restored');
    }, function() {
        console.log('Not fired due to the catch'); // 再次绑定捕获也不会再次被触发
    });

    // The following behaves the same as above
    p1.then(function(value) {
        console.log(value); // "Success!"
        return Promise.reject('oh, no!'); // 再次发起一个异常
    }).catch(function(e) {
        console.log(e); // "oh, no!"
    }).then(function() {
        console.log('after a catch the chain is restored');
    }, function() {
        console.log('Not fired due to the catch'); //同样也不会被捕获
    });

如果强制抛出了两次异常,例如:

var pro = new Promise(function(resolve, reject){
    setTimeout(function(){
        reject(new Error('this is error'));
    },1000);
}).then(function(arg){
    return Promise.reject('this is error 2');
}).catch(function(arg){
    console.log(arg); // Error('this is error')
}).catch(function(arg){
    console.log(arg); //undefined
});

这种场景下只有第一个异常会被捕获

3. Promise.all 监听多个promise的状态
// `delay`毫秒后执行resolve
function timerPromisefy(delay) {
    return new Promise(function (resolve) {
        setTimeout(function () {
            resolve(delay);
        }, delay);
    });
}
var startDate = Date.now();
// 所有promise变为resolve后程序退出
Promise.all([
    timerPromisefy(1),
    timerPromisefy(32),
    timerPromisefy(64),
    timerPromisefy(128)
]).then(function (values) {
    console.log(Date.now() - startDate + 'ms');  // 約128ms
    console.log(values);    // [1,32,64,128]
});

必须等到所有的promise 的状态都变成FulFilled 之后才会触发all的成功方法

4. Promise.race 监听多个promise
// `delay`毫秒后执行resolve
function timerPromisefy(delay) {
    return new Promise(function (resolve) {
        setTimeout(function () {
            resolve(delay);
        }, delay);
    });
}
// 任何一个promise变为resolve或reject 的话程序就停止运行
Promise.race([
    timerPromisefy(1),
    timerPromisefy(32),
    timerPromisefy(64),
    timerPromisefy(128)
]).then(function (value) {
    console.log(value);    // => 1
});

只要有任意的一个promise对象变成FulFilled状态,then中的方法就会被调用

再看看如果第一个promise执行完之后,其他的promise会不会继续执行

var winnerPromise = new Promise(function (resolve) {
        setTimeout(function () {
            console.log('this is winner');
            resolve('this is winner');
        }, 4);
    });
var loserPromise = new Promise(function (resolve) {
        setTimeout(function () {
            console.log('this is loser');
            resolve('this is loser');
        }, 1000);
    });
// 第一个promise变为resolve后程序停止
Promise.race([winnerPromise, loserPromise]).then(function (value) {
    console.log(value);    // => 'this is winner'
});

从结果来看其他的promise还是会继续执行的

5. 使用reject而不是throw

参考: http://liubin.org/promises-book/#not-throw-use-reject

Promise的构造函数,以及被 then 调用执行的函数基本上都可以认为是在 try...catch 代码块中执行的,所以在这些代码中即使使用 throw ,程序本身也不会因为异常而终止。

如果在Promise中使用 throw 语句的话,会被 try...catch 住,最终promise对象也变为Rejected状态。

var promise = new Promise(function(resolve, reject){
    throw new Error("message");
}).catch(function(error){
    console.error(error);// => "message"
});

使用reject的优点

  • 如果使用throw的话很难区分是我们主动抛出来的还是真正的其他异常,在Chrome中有 Pause On Caught Exceptions 不能区分
  • 在 then 中使用reject的方法
var onRejected = console.error.bind(console);
var promise = Promise.resolve();
promise.then(function () {
    return Promise.reject(new Error("this promise is rejected"));
}).catch(onRejected);

这样在 then 方法中可以很方便的控制流程,同时传递任意的参数

6. 最佳实践

实现一个超时调用

function delayPromise(ms) {
    return new Promise(function (resolve) {
        setTimeout(resolve, ms);
    });
}

delayPromise(100).then(function () {
    console.log("已经过了100ms!");
});

这样的超时调用更加的优雅

转载于:https://www.cnblogs.com/koffee/p/5309079.html

系统支持前后端分离架构,涵盖微信、支付宝、百度、头条等主流平台的小程序、APP及公众号,内置多种常见支付方式,具备完善的订单处理机制,界面设计美观,是一款功能完备的商城开源平台。毕业设计是高校教育中的一项关键实践性任务,用于评估学生在专业领域内的知识掌握程度、实践能力和创新思维。该任务通常要求学生结合所学理论,针对某一具体问题提出可行的解决方案或开展一项具有实际价值的研究项目。 在选题阶段,学生需根据个人兴趣、专业方向及现实需求进行选择,并在导师指导下明确研究目标与核心问题,制定研究计划与实施方案。整个过程通常包含资料查阅、需求分析、系统设计、开发实现及测试优化等多个环节,确保研究的完整性与科学性。 在研究过程中,学生需具备较强的自主分析与问题解决能力,可能通过实验、调研、案例研究等方式收集数据并验证假设,从而提升专业技能与实际操作能力。撰写毕业设计报告是核心环节之一,需详细记录研究过程、方法、结果及结论,以全面展示研究成果。同时,这一过程也有助于提升学生的学术表达能力与逻辑思维水平。 最终,毕业设计成果将由导师及相关专家进行评审,评价标准涵盖创新性、应用价值、研究方法的合理性及论文撰写质量等方面。毕业设计的成绩将作为学生学业评估的重要依据,直接影响其毕业资格与学位授予。 总体而言,毕业设计是高校教学体系中的重要组成部分,不仅有助于学生深化专业知识,还能锻炼其独立研究与实践能力,为未来职业发展奠定良好基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值