DEV Community

Discussion on: How I use JavaScript Promises

Collapse
 
cuitpoisson profile image
cuitpoisson

Maybe the point of the article was to show how simple it is to wrap everything in a promise, but starting with the original:

function getRollNumberFromName(studentName) {
    return new Promise(function(resolve, reject) {
        fn_to_get_roll_number_from_db(studentName)
            .then((rollNumber) => {
                resolve(rollNumber);
            })
            .catch((err) => {
                reject(err);
            });
    });
}

And combining with your suggestion (and doing the same with reject):

function getRollNumberFromName(studentName) {
    return new Promise(function(resolve, reject) {
        fn_to_get_roll_number_from_db(studentName)
            .then(resolve)
            .catch(reject);
    });
}

We can see that the then is just returning the value from the original promise and the catch is also just rejecting with the already rejected value. That means we're just returning the original promise, which can be simplified to:

function getRollNumberFromName(studentName) {
    return fn_to_get_roll_number_from_db(studentName);
}

Which is just the fn_to_get_roll_number_from_db function.