The purpose of return is to stop the function from being rejected and to prevent code from executing after it.
function divide(numerator, denominator) { return new Promise((resolve, reject) => { if (denominator === 0) { reject("Cannot divide by 0"); return;
In this case, it prevents the execution of resolve(numerator / denominator); that is strictly not required. However, it is still preferable to stop execution to prevent a possible trap in the future. In addition, it is good practice to prevent code uptime.
Background
A promise can be in one of three states:
- deferred - initial state. From the waiting, we can go into one of the other states.
- done - successful operation
- Failed - failed operation
When a promise is fulfilled or rejected, it will remain in this state for an indefinite time (settled). Thus, the rejection of a fulfilled promise or the fulfillment of a rejected promise will not affect.
This example fragment shows that although the promise was fulfilled after rejection, it remained rejected.
function divide(numerator, denominator) { return new Promise((resolve, reject) => { if (denominator === 0) { reject("Cannot divide by 0"); } resolve(numerator / denominator); }); } divide(5,0) .then((result) => console.log('result: ', result)) .catch((error) => console.log('error: ', error));
So why should we come back?
Although we cannot change the state of the promised promise, rejection or resolution will not stop the rest of the function. A function may contain code that creates confusing results. For example:
function divide(numerator, denominator) { return new Promise((resolve, reject) => { if (denominator === 0) { reject("Cannot divide by 0"); } console.log('operation succeeded'); resolve(numerator / denominator); }); } divide(5, 0) .then((result) => console.log('result: ', result)) .catch((error) => console.log('error: ', error));
Even if the function does not contain such code right now, this creates a possible future trap. A future refactor may ignore the fact that the code is still executed after the promise is rejected, and it will be difficult for us to debug.
Stop execution after authorization / rejection:
This is standard JS control flow material.
- Return after
resolve / reject :
function divide(numerator, denominator) { return new Promise((resolve, reject) => { if (denominator === 0) { reject("Cannot divide by 0"); return; } console.log('operation succeeded'); resolve(numerator / denominator); }); } divide(5, 0) .then((result) => console.log('result: ', result)) .catch((error) => console.log('error: ', error));
- Return using
resolve / reject - since the return value of the callback is ignored, we can save the string by returning the reject / resolve statement:
function divide(numerator, denominator) { return new Promise((resolve, reject) => { if (denominator === 0) { return reject("Cannot divide by 0"); } console.log('operation succeeded'); resolve(numerator / denominator); }); } divide(5, 0) .then((result) => console.log('result: ', result)) .catch((error) => console.log('error: ', error));
- Using the if / else block:
function divide(numerator, denominator) { return new Promise((resolve, reject) => { if (denominator === 0) { reject("Cannot divide by 0"); } else { console.log('operation succeeded'); resolve(numerator / denominator); } }); } divide(5, 0) .then((result) => console.log('result: ', result)) .catch((error) => console.log('error: ', error));
I prefer to use one of the return options, as the code is flatter.
Ori Drori Sep 12 '15 at 6:50 2015-09-12 06:50
source share