美文网首页
promise 执行多个函数练习

promise 执行多个函数练习

作者: 糖醋里脊120625 | 来源:发表于2019-08-07 11:48 被阅读0次
    let checkLogin = function() {
        return new Promise(function(resolve, reject) {
            let flag = document.cookie.indexOf("userId") > -1 ? true : false;
                console.log("执行步骤2")
            if(flag = true) {
                resolve({
                    status: 0,
                    result: true
                })
            } else {
                reject("error");
            }
    
        })
    };
    
    let getUserInfo = () =>{
        return new Promise((resolve, reject) => {
            let userInfo = {
                userId: "101"
            }
            resolve(userInfo);
        });
    }
    
    checkLogin().then((res) => {
        console.log("执行步骤1")
        console.log(res)
        if(res.status == 0) {
            console.log("login success");
            return getUserInfo();
        }
    }).catch((error) => {
        console.log(`errrs:${error}`)
    }).then((res2) => {
        console.log(`userId:${res2.userId}`)
    });
    
    Promise.all([checkLogin(), getUserInfo()]).then(([res1, res2]) => {
        console.log(`result1:${res1.result},result2:${res2.userId}`)
    })
    

    相关文章

      网友评论

          本文标题:promise 执行多个函数练习

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