I'm refactoring my code tested with Mocha and theres a piece of code I don't quite understand.
Test.js
step(`Some description, blah blah blah.`, () => new Promise(function(resolve, reject) {
let temp = myObject.recursiveMethod();
console.log(temp);
resolve(temp);
}));
MyObject' method
async recursiveMethod() {
//refactored alot of promises into awaits
await doStuff();
if(someCalculation()) {
await cleanUp();
return this.recursiveMethod();
}
},
When such code is executed, everything is fine, however I'd like to get rid of RETURN in the method call - in the end the function call at the promise is going to get undefined anyway, right? Unfortunately, in that case the test step gets terminated. What could be helpful is that in the case with return, the console outputs a promise which is pending for the time of recurrence, after that gets resolved, in case without return the promise is resolved immediately.
Could someone explain me the difference? Even without the promise, the function shouldn't finish without executing the last instruction, should it?
EDIT. I've changed the code to remove the redundant promise, which previously I considered neccesary. Now the test looks as follows:
step(`Some description, blah blah blah.`, async() => await myObject.recursiveMethod());
What's interesting, I had to wrap the function call in lambda, despite the method being async itself. Anyway, I still don't understand how does removing the RETURN keyword (which doesn't return any value in any case) affect the code execution, which is different.