Es6 promise and async usage issues

promise implements two requests, and the third step is to show the result. For the second request, you can get the result of the first request, but when you get to the third step, the res is not the res, returned by the second request.

let a=new Promise((res,rej)=>{
                //
                setTimeout(()=>{
                    res("data")
                })
            })
            a.then((res)=>{
            //
                setTimeout(()=>{
                    console.log("");
                },3000)
            }).then((res)=>{
            //
                console.log(res+"");
            })

if this function is changed to write in asyncawait,

function a() {
                return new Promise((res, rej) => {
                    setTimeout(() => {
                        console.log("1");
                        res(1)
                    }, 2000)
                })
            }
            function c() {
                return new Promise((res) => {
                    setTimeout(() => {
                        console.log("2");
                        res(2)
                    }, 3000)
                })
            }
            async function b() {
                await a()
                await c()
            }
            let d=b()

c is originally executed in a, but after an executes, it takes the parameters from a to make a request or other operation. Now it is also in a mode similar to Synchronize, how can c get the parameters that were originally sent by a.

Jul.12,2021

the first one is modified on the basis of the original code. can extract the p2 method on its own :

let a = new Promise((res,rej)=>{
    //
    setTimeout(()=>{
        res('')
    }, 1000)
})
a.then((res)=>{
//
    console.log(res);
    const p2 = new Promise (function(resolve, reject){
     setTimeout(()=>{
          resolve('');
      },2000)
    })
    return p2; // Promise
}).then((res)=>{
    // 
    console.log(res); // 
})

second, use the method of await/async :

const a = new Promise((res,rej)=>{
    //
    setTimeout(()=>{
        res('')
    }, 1000)
})

const b = new Promise (function(resolve, reject) {
    setTimeout(()=>{
       resolve('');
    },2000)
})

const process = async function () {
    const step_1_result = await a;
    console.log(step_1_result);    
    const step_2_result = await b;
    console.log(step_2_result); 
    return step_2_result + '/process'
    // ps: return:
    // return Promise.resolve(step_2_result + '/process')
}

process().then(function (res) {
    console.log(res) // process
})

function requestSth() {
    return new Promise((res,rej)=>{
        setTimeout(()=>{
            res('data')
        }, 1000)
    })
}
function requestSth2() {
    return new Promise((res,rej)=>{
        setTimeout(()=>{
            res('data2')
        }, 2000)
    })
}
function b(x, y) {
    console.log(x, y)
}
async function a() {
    let data = await requestSth()
    let data2 = await requestSth2()
    b(data, data2)
}
a()
Menu