美文网首页
Promise基础

Promise基础

作者: 大菜鸟呀 | 来源:发表于2018-09-02 00:47 被阅读2次
基本使用:
    var a=1;
        new Promise((resolve,reject)=>{
            setTimeout(()=>{
                a=10;
                resolve(); 改为成功状态
            },2000)
        }).then(()=>{
            console.log(a)
        })
--------------------------------------------
        var a=1;
        new Promise((resolve,reject)=>{
            setTimeout(()=>{
                a=10;
                reject();//改为失败状态
            },2000)
        }).then(()=>{
            console.log(a)
        },()=>{
            console.log('失败状态')  在then的第二个参数中体现
        })

值的传递:

        
        new Promise((resolve,reject)=>{
            //resolve,reject 可以传递参数,传递的参数 给后面的then()使用
            setTimeout(()=>{
                var a=10;
                resolve(a)
            
            },2000)
        }).then((a)=>{
            console.log(a)
        },()=>{
            console.log('失败状态')
        })

连续出发的状态:

        new Promise((resolve,reject)=>{
            resolve()
        }).then(()=>{
            console.log(1)
        },()=>{
            console.log('2')
        }).then(()=>{
            console.log(3)
        },()=>{
            console.log('4')
        })
返回:1、3
--------------------------------------
        new Promise((resolve,reject)=>{
            reject()
        }).then(()=>{
            console.log(1)
        },()=>{
            console.log('2')
        }).then(()=>{
            console.log(3)
        },()=>{
            console.log('4')
        })
返回:2、3
---------------------------------------
        new Promise((resolve,reject)=>{
            reject()
        }).then(()=>{
            console.log(1)
        },()=>{
            console.log(2);
            return new Promise((resolve,reject)=>{
            reject();
            })
        }).then(()=>{
            console.log(3)
        },()=>{
            console.log(4)
        })
返回:2、4

铺获失败,并终止执行:

        new Promise((resolve,reject)=>{
            resolve()
        }).then(()=>{
            console.log(1);
            return new Promise((resolve,reject)=>{
            reject('失败了');
            })
        }).then(()=>{
            console.log(3)
        }).catch((err)=>{
            console.log(err)
        })
在1中失败以后,3不再执行
注意: catch以后的then还是会执行的,catch铺获的是所在位置以前的错误

相关文章

  • Promise

    promise基础

  • 第八周第二天笔记

    ES6之Promise类 1 Promise类基础知识解读 promise类的静态属性方法分类:resolve()...

  • Promise

    基础用法 Promise.all() Promise.all方法用于将多个 Promise 实例,包装成一个新的 ...

  • js-Promise基础

    js-Promise基础(第三周) Promise含义 promise意思为允诺、许诺、承诺在js中为了解决编写代...

  • 2020前端基础大纲(20200202)

    2020前端基础大纲1、promise 原理 promise.all 可以手写出大概。(async awa...

  • Promise基础

    值的传递: 连续出发的状态: 铺获失败,并终止执行:

  • Promise基础

    Promise Promise对象的三种状态 Promise状态的改变形式 Promise基本的api eg.1 ...

  • Promise基础

    1、Promise是什么? promsie是ES6新增的一个特性,它已经列入ES6的正式规范中promise是抽象...

  • Promise基础

    什么是promise 抽象表达Promise是JS中进行异步编程的新的解决方案(Promise也支持回调),旧的方...

  • promise基础

    promise有两个阶段:未决阶段unsettled和已决阶段settled promise有三种状态:pendi...

网友评论

      本文标题:Promise基础

      本文链接:https://www.haomeiwen.com/subject/mzepwftx.html