Introduction to Promises

 

unfinished post

I decided to write a Telegram Bot using NodeJS, but nearly every library I use are async. It seems I have to learn some promises…

Callback function

Unlike other languages, async functions in Javascript seldom “return” values. Instead, they pass the result to another given function named callback to “return” the result.

Example of sync functions

function doSomething(param){
    //do things
    return res;
}

Example of async functions

function asyncDo(param){
    return new Promise(
        (fufill, reject) => {
            //do things
            if (err) reject(err);   //If things went wrong, throw error to reject()
            else fufill(res)
        }
    );
}