Using Async/Await in Nodejs

Keywords: Programming JSON

async function rejectionWithReturn(){
    try{
        return Promise.reject(new Error())
    } catch (e){
        return "Saved"
    }
}
rejectionWithReturn().then((ret) => {
    console.log(ret)
})

//a. the function can be changed into an async function by the keyword async before the function body

//b. in the async function, the await keyword should be added before the function to be executed asynchronously

//C. functions after await must be encapsulated with Promise object

//D. the async function returns a Promise object after execution

 

var delay_print_first = function(){
    return "First";
}
var delay_print_second = function(){
    return Promise.resolve("Second")
}
var delay_print_third = function(){
    return Promise.resolve("Third")
}
var async_status = async function(ms){
    try{
        var first = await delay_print_first();
        var send = await delay_print_second();
        var third = await delay_print_third();
        return first + " " + send + " " + third;
    } catch (error) {
        return Promise.reject("Some error")
    }
}
async_status().then((ret) => {
    console.log(ret)
}).catch((err) => {
    console.log(err)
})

//In order to effectively record the error information, we usually do some Promise catch processing after async execution


 

var Delay_Time = function(ms) {
    return new Promise(function(resolve){
        setTimeout(resolve, ms)
    })
}
var Delay_Time_Second = function(ms){
    setTimeout(function(){}, ms)
}
var Delay_Print = async function(ms){
    Delay_Time_Second(2000)
    console.log("After Delay_Time_Second")
    await Delay_Time(ms)
    console.log("After Delay_Time")

    return "END"
    // return Promise.resolve("END")
    // return await "END"
    // return new Promise(function(resolve, reject){
    //     resolve("END")
    // })
}
Delay_Print(2000).then(function(resolve){
    console.log(resolve)
})

//Under normal circumstances, await is followed by a Promise object. If it is not, it will be immediately converted into a Promise object of immediate resolution


 

var delay_time = function(ms, param) {
    return new Promise(function(resolve){
        setTimeout(function(){
            console.log(new Date().getTime())
            resolve(param)
        }, ms)
    })
}
var asyn_fun = async function(param) {
    var time_out = 1000;
    const results = await param.map(async param => {
        time_out = time_out + 1000;
        var out = await delay_time(time_out, param)
        return out
    })
    var target = [];
    for (var ret of results){
        target.push(await ret)
    }
    return await target
}
asyn_fun(['First', 'Second', 'Third', 'Last']).then(function(result){
    console.log(JSON.stringify(result))
})

//Although the parameters of the map method are async functions, they are executed concurrently, because only async functions are executed internally and externally. The following for..of loop uses await internally, so sequential output is implemented


 

async function rejectionWithReturnAwait() {
    try{
        return await Promise.reject(new Error())
    } catch (e){
        return "Saved"
    }
}
rejectionWithReturnAwait().then((ret) => {
    console.log(ret)
})

//This code catches the project object declared by await and its status is rejected through try/catch in the async method body, and returns the project object executed immediately after catching the exception


 

 

//The project object in the async code block above is not declared with the await keyword, because when the state of the project object changes from pending to rejected, it cannot be tried / catch caught

Posted by benyhanna on Wed, 04 Dec 2019 16:55:43 -0800