0

I know about code after resolve will be executed; I'm not talking about that. My question is about returning a resolve object like:

return resolve(err)

Is it different from returning nothing? Like:

resolve(err)
return

which is like

  ...
  resolve(err)
}

I'm talking about the canonical case where the promise is handled with then and catch like:

functionReturningAPromise().then().catch()
Curcuma_
  • 851
  • 2
  • 12
  • 37
  • It happened to me to write code like that, and I did it just to use one line in place of two. It can be useful when you don't want to use brackets for if, like: `if (err) return resolve(err);`. – Christian Vincenzo Traina Apr 11 '22 at 21:13
  • I do that as well, and sometimes both. until now it popped to my eyes, but still not convinced with @jabaa answer. See my comment – Curcuma_ Apr 11 '22 at 21:15
  • There is actually discussion of your question in the comments in this duplicate [Why does javascript ES6 Promises continue execution after a resolve?](https://stackoverflow.com/questions/28896280/why-does-javascript-es6-promises-continue-execution-after-a-resolve) – pilchard Apr 11 '22 at 21:27
  • that is not the same ! I know code continues execution. (ah in comments, right right) – Curcuma_ Apr 11 '22 at 21:29

1 Answers1

3

The statement

return resolve(err);

doesn't make much sense and could be confusing. resolve doesn't explicitly return anything. The only reason to use

return resolve(err);

is to shorten

resolve(err);
return; 

That means that

return resolve(err);

and

resolve(err); 
return;

are equivalent.

jabaa
  • 5,844
  • 3
  • 9
  • 30
  • 1
    well from JavaScript perspective, they are not the same, one is returning "something," the other is returning nothing. I know this whole implementation is wrapped in what we call a Promise. So now the question are Promises implemented to ignore what we return ? I hope you see what I'm referring to. THEY ARE NOT THE SAME from JS perspective. – Curcuma_ Apr 11 '22 at 21:13
  • 1
    @Curcuma_ Both return the same `undefined`. A return statement without return value returns `undefined`. `resolve` implicitly returns `undefined` and `return resolve(err)` explicitly returns `undefined`. Same result. No difference. – jabaa Apr 11 '22 at 21:13
  • 1
    Arguably they return different `undefined`s ;) – pilchard Apr 11 '22 at 21:15
  • you cannot return two times inside one block. @jabaa you are too convinced but wrong. `resolve` does not `return` – Curcuma_ Apr 11 '22 at 21:17
  • @Curcuma_ You can be sure I would already have gotten the first downvote, if both weren't equivalent. `resolve` doesn't return from the function. It **is** a function with a return value. `resolve` implicitly returns `undefined`. Try `console.log(resolve(err));` – jabaa Apr 11 '22 at 21:17
  • 1
    Did i say they were magic? I was merely making the semantic observation that the first returns the return value of `resolve()` (which is undefined) while the second returns it's own undefined. (regardless of the fact that they are indistinguishable from each other, thus the wink) – pilchard Apr 11 '22 at 21:18
  • @Curcuma_ In addition, the returned value is discarded. `resolve(err); return 123;` has the same behavior. You can't access the value `123`. More details about the promise executer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise – jabaa Apr 11 '22 at 21:25
  • @jabaa I understand that. Still 123 is returned but could not be exploited (value or pointer if object is lost because of the implementation of promises). – Curcuma_ Apr 11 '22 at 21:27
  • Your answer is correct but @pilchard who proved why. thnx :) – Curcuma_ Apr 11 '22 at 21:27