28.Promise链式调用的写法

2019-11-26  本文已影响0人  最爱喝龙井

promise链式调用resolve的写法

有三种方式:

//1. 原始写法
        let foo = new Promise((resolve) => {
            setTimeout(() => {
                resolve('aaa');
            }, 1000)
        });

        foo.then(res => {
            console.log(res);
            return new Promise(resolve => {
                resolve(res + 111);
            })
        }).then(res => {
            console.log(res)
        })


        //2. 第二种写法
        let foo = new Promise((resolve) => {
            setTimeout(() => {
                resolve('aaa');
            }, 1000)
        });

        foo.then(res => {
            console.log(res);
            return Promise.resolve(res + 111)
        }).then(res => {
            console.log(res)
        })

        //3.第三种写法
        let foo = new Promise((resolve) => {
            setTimeout(() => {
                resolve('aaa');
            }, 1000)
        });

        foo.then(res => {
            console.log(res);
            return res + 111
        }).then(res => {
            console.log(res)
        })
上一篇下一篇

猜你喜欢

热点阅读