0

If I want to use a Promise to ensure a function is run first but don't care what the return is, what do I resolve it to? Is it best practice to resolve(true)?

let getShoes = return new Promise((resolve, reject) => {
    // do something 
    //resolve(true);  resolve using true?
});

getShoes.then(() => {
    // do something else but don't need the return from getShoes...only need to ensure it is run first
})
MarketMan39
  • 188
  • 1
  • 7
  • 1
    No need for `return new Promise`...just `return fetch`. fetch is promise based. – David784 Dec 05 '20 at 21:54
  • true @David784 . What about if it wasn't using fetch and rather just doing a calculation first where I didn't care about the return but just wanted to make sure it ran first? – MarketMan39 Dec 05 '20 at 22:06
  • I edited the question to make it w/o making an API call – MarketMan39 Dec 05 '20 at 22:09
  • I think that undefined is better, in async function undefined is the default returned value, just like regular functions – Sarabadu Dec 05 '20 at 22:17
  • It's been a while back, (node 8 maybe?) but I once had a node.js project where I tried just calling `resolve()` with no value (undefined), and weird things happened. When I added a return value, the weirdness stopped. Might've just been a bug, and maybe it's not an issue any more. But ever since then I always make sure to have some value in resolve and reject, even if it's just a boolean or integer or whatever. Just my $0.02 – David784 Dec 05 '20 at 22:21
  • Ohh didn’t know that @David784 – Sarabadu Dec 05 '20 at 22:25

2 Answers2

1

I will choose to return undefined.

A function without return statement or with empty return statement always return undefined

The same happens with async functions.

If you resolve with empty resolve() you get undefined as fulfillment value.

Sarabadu
  • 587
  • 2
  • 18
-1

Resolving to true sounds reasonable to me because you'll explicitly state that you don't care about the return value of the promise.

You could also invoke resolve() without parameters as in Promise.resolve with no argument passed in, which will resolve to undefined. By doing that, you will again be stating that you don't care about the value.

Turtlean
  • 579
  • 4
  • 9
  • I didn't explicitly mention that calling `resolve()` without parameters returns `undefined`, but it is mentioned in the thread that I linked to. I edited the answer to include that part – Turtlean Dec 05 '20 at 22:35