promise 的基本用法


//知识点1 例1--- 最基本的写法 Promise的基本语法哦 const Aa=new Promise(function(resolve,reject){ //resolve和reject是参数也是方法 if(true){ resolve("这是成功的数据哦") }else{ reject("这是失败的数据") } }); Aa.then(function(data){ // data是成功的数据,是上一步中resolve()方法处理的数据 console.log(data); //若果成功,输出这这一句代码哦 输出“这是成功的数据哦 }, function(err){ // err就是失败的数据 其实就是上一步使用 reject() 方法处理的数据 console.log(err); } )
     new Promise((resolve, reject) => {
            if (true) {
                resolve('成功的数据')
            } else {
                reject('失败的数据')
            }
        }).then((data) => {
            console.log(data)  //输出 成功的数据
        }, (err) => {
            console.log(err)
        }) 
原文地址:https://www.cnblogs.com/IwishIcould/p/11560396.html