-2

Is there any difference between the following two definitions?

const makeGuess = async function (guess) {
    const answer = await getAnswer(); // assume no error here
    if (guess == answer) {
        return Promise.resolve('yay');
    } else {
        return Promise.reject('boo');
    }
};

const makeGuess = function (guess) {
    return new Promise(async function(resolve, reject) {
        const answer = await getAnswer(); // assume no error here
        if (guess == answer) {
            resolve('yay');
        } else {
            reject('boo');
        }
    };
};
drmrbrewer
  • 11,491
  • 21
  • 85
  • 181
  • 1
    In the second case, if there is any error in `getAnswer`, it will not be caught when you `await makeGuess()`. It will not propagate the error. [Is it an anti-pattern to use async/await inside of a new Promise() constructor?](https://stackoverflow.com/questions/43036229) – adiga Jan 30 '21 at 10:50
  • @adiga it was just a simplified example... the assumption is that there is no error in `getAnswer`... it is just meant to resolve or reject based on whether the guess matches the answer – drmrbrewer Jan 30 '21 at 10:58

2 Answers2

1

If you are using async funcion, you don't need to return a promise. Anything returned from async function will be promise.

const makeGuess = async function (guess) {
   const answer = await getAnswer();
   
   if (guess == answer) {
       return 'yay';
   } else {
       throw 'boo'
   }
};

makeGuess('test')
   .then(success => console.log(success)) // yay - if you return
   .catch(err => console.error(err)) // boo - if you throw
Dejan Sandic
  • 451
  • 2
  • 10
  • But does your reformulated function actually do exactly the same as my first example, or is there a difference? – drmrbrewer Jan 30 '21 at 10:31
  • It should do exactly the same thing – Dejan Sandic Jan 30 '21 at 13:31
  • So which is "better"... yours, or my first example, or my second example? Is any one better than any other or is it just a matter of choice? – drmrbrewer Jan 30 '21 at 13:42
  • In your examples, you are unnecessarily returning promises from the async function that returns a promise anyway. So my example is more concise and without unnecessary code. – Dejan Sandic Jan 30 '21 at 14:48
  • OK maybe sometimes it helps to be more explicit about what is being returned? Makes it more clear that you are indeed returning a promise and not a plain value. In any case, my original question is not how to make the first example more concise, but actually whether there is any meaningful difference between the first and second examples, or whether they both achieve exactly the same result in terms of async performance. – drmrbrewer Jan 30 '21 at 15:31
1

The async/await feature was built on top of promises. To be clear, there isn't much of a difference between them. To quote the NodeJS learning article on Async/Await (https://nodejs.dev/learn/modern-asynchronous-javascript-with-async-and-await)

They reduce the boilerplate around promises, and the "don't break the chain" limitation of chaining promises.

Do take a look at the above link to understand the concept better.

Black Wind
  • 313
  • 1
  • 8