Lets say I have a Promise like the following:
new Promise((resolve, reject) => {
// Some random conditional
if (today === 'Friday') {
console.log("I'm resolving this thing here");
resolve('taco');
// Should I have a return true or false here?
}
console.log("Code made it past the first resolve");
// Do some other code here, like connect to a database or API or something.
connectToDatabase(function(result){
// result was 'hamburger'
resolve(result);
});
}).then((value) => {
console.log(value);
});
In this example, I get the following output:
I'm resolving this thing here
Code made it past the first resolve
taco
So, after I do a resolve
, the Promise code continues to execute. In my above example, I unnecessarily connect to some database or endpoint or something. Obviously the send resolve
is ignored, which is just fine. But connecting to databases and APIs is expensive from a service cost standpoint, especially if it costs you money every time you make a request like an AWS service or something.
So my question is, should I be putting in a return true;
or something in after that first resolve
?
Similarly with reject
, should I put in a return false
or (true??) after a reject
?
new Promise((resolve, reject) => {
someAsyncFunction((err, result) => {
if (err) {
reject(err);
// Should I have a return true or false here or something?
}
// Do something here, like connect to a database or fetch a value from some API endpoint or something.
resolve(result);
});
});
If I don't want the code to be executed after that reject
I should return, correct?
When I look through all sorts of ES6 documentation, I can't find any places where they make this clear... Lots of Promise examples are usually really simplified.
If I do return something on these, what should I return? true
? false
? or return resolve('taco');
?