DEV Community

Discussion on: 3 most common mistakes when using Promises in JavaScript

Collapse
 
axtn profile image
Alex T • Edited

Async functions also return promises that resolve with the return value.

const wait = () => new Promise(resolve => {
    setTimeout(resolve, 3000);
});

const waitAndReturn = async () => {
    await wait();
    // throw new Error('oops!'); throwing rejects
    return 'hi';
};

So the last example can even be written like:

const createSomePromise = async () => {
  // make HTTP request
  return result;
};
Collapse
 
7qruzer profile image
Kumar Gaurav • Edited

Small correction here ...

const createSomePromise = async () => {
  // await HTTP request's response
  return result;
};
Collapse
 
axtn profile image
Alex T

Thanks for making that clearer Kumar!