NodeJS - Example of Promises in ES6


Here, I’ve added a simple example of usage of Promise in javascript for Node.js.

var asyncAdd = (a, b) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (typeof a === 'number' && typeof b === 'number') {
                resolve(a + b);
            } else {
                reject('Arguments must be numbers');
            }
        }, 1500);
    });
}

asyncAdd(10, 15).then((result) => {
    console.log('Result: ', result);
}, (error) => {
    console.log(error);
});

Above function can be chained with multiple calls. Here is an example.

asyncAdd(10, 15).then((result) => {
    console.log('Result: ', result);
    return asyncAdd(result, 33); 
}, (error) => {
    console.log(error);
}).then((result) => {
    console.log('Result: ', result);
}, (error) => {
    console.log(error);
});

But such chainings are not recommended. For better usage, use catch block in Promise.

asyncAdd(10, '15').then((result) => {
    console.log('Result: ', result);
    return asyncAdd(result, 33); 
}).then((result) => {
    console.log('Result: ', result);
}).catch((error) => {
    console.log(error)
});