zoukankan      html  css  js  c++  java
  • promise 笔记

    拒绝

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

                throw new Error(0);

            });

            promise.then(function(arg){

                console.log(arg);

            }).catch(function(arg){

                console.log('c' + arg);//走这

            });

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

                reject(0);

            });

            promise.then(function(arg){

                console.log(arg);

            },function(arg){

                console.log('j' + arg);//走这

            }).catch(function(arg){

                console.log('c' + arg);

    异常

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

                throw new Error(0);

            });

            promise.then(function(arg){

                console.log(arg);

            }).catch(function(arg){

                console.log('c' + arg);//走这

            });

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

                reject(0);

            });

            promise.then(function(arg){

                console.log(arg);

            },function(arg){

                console.log('j' + arg);//走这

            }).catch(function(arg){

                console.log('c' + arg);

    then() cath()会构建和返回一个promise实例

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

                throw new Error('x');

            });

            promise.then(function(arg){

                console.log(arg);

                return 'xx';//返回给下一个thenresolve(抛异常也是下一个thenresolve

            },function(arg){

                console.log('reject' + arg);

                return 'xx';//返回给下一个thenreject

            }).catch(function(arg){

                console.log('cathc' + arg);

            })

            .then(function(data){

                console.log('last resolve:' + data);

            },function(data){

                console.log('last reject:' + data);

            });

    延迟,异步异常

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

                setTimeout(function() {

                    reject('x');//可以捕获,如果是 throw 一个error就无法捕获

                });

            });

            promise.then(function(arg) {

                    console.log('resole:' + arg);

                }, function(arg) {

                    console.log('reject' + arg);

                })

                .catch(function(arg) {

                    console.log('cathc:' + arg);

                })

    补充:

    window.addEventListener("unhandledrejection", function (event) {

      console.warn("WARNING: Unhandled promise rejection. Shame on you! Reason: "

                   + event.reason);});

    用来捕获拒绝,window.onerror只是捕获异常不会捕获promise的拒绝

  • 相关阅读:
    【前端】Node.js学习笔记
    【前端】Web前端学习笔记【2】
    【Python】我的Python学习笔记【2】【using Python 3】
    【iOS】Foundation框架 学习笔记
    了解数据库对象
    对Bootstrap的粗略认识
    win10电脑突然开不了热点,怎么办
    excel 怎么将表中的空格都转变为0???
    cpno在数据库里面是什么意思??
    ·ps 的暂存盘空间不够
  • 原文地址:https://www.cnblogs.com/c-o-d-e/p/6642412.html
Copyright © 2011-2022 走看看