Sandra Schlichting
Sandra Schlichting

Reputation: 26056

How to combine async/await with Promises?

In the below code have I createPost() inside an async function, but I don't want to use await on it, so it blocks execution of the rest of the function. That is why I used then.

The outer async function have lots of other functions that use await, so that can't be changed.

Question

Since createPost() is a async/await function (see below). How can it resolve/reject to then/catch in the outer function?

module.exports = async (p) => {
  // lots of code here
  
 const t = await function1();      

    // lots of code here
    createPost(p).then(message => {
      // lots of code here
      console.log("ok " + message);
    }).catch(message => {
      console.log("failed " + message);
    });
};

createPost()

module.exports = async (p) => {
  // lots of code here
  try {
    const r = await getStat();
  } catch (error) {
    console.log(error);
  };
};

Upvotes: 0

Views: 2218

Answers (2)

Ran Turner
Ran Turner

Reputation: 18156

An async decleration on a function is redundant and wrong if you're not using the await keyword inside.

await/async are often referred to as syntactic sugar, and let us wait for something (e.g. an API call), giving us the illusion that it is synchronous in an actual asynchronous code, which is a great benefit.

The things you want to acheive with async/await is is possible with promises but the advantages of async/await. let's an example with this code:

const makeRequest = () => //promise way
  getJSON()
    .then(data => {
      return data
    })

makeRequest();

const makeRequest = async () => { //async await way
  const data = await getJSON();
  return data;
 }

makeRequest()

Why is async/await prefered over promise?

  1. Concise and clean - We didn’t have to write .then and create an anonymous function to handle the response, or give a name data to a variable that we don’t need to use. We also avoided nesting our code. async/await is a lot cleaner.

  2. Error handling - Async/await makes it finally possible to handle both synchronous and asynchronous errors with the same try/catch format.

  3. Debugging - A really good advantage when using async/await is that it’s much easier to debug then promises for 2 reasons: 1) you can’t set breakpoints in arrow functions that return expressions (no body). 2) if you set a breakpoint inside a .then block and use debug shortcuts like step-over, the debugger will not move to the the following .then because it only “steps” through synchronous code.

  4. Error stacks - The error stack returned from promises chain gives us no idea of where the error occured and can be misleading. async/await gives us the error stack from async/await points to the function that contains the error which is a really big advantage.

Upvotes: 1

tomleb
tomleb

Reputation: 2533

async / await is a more modern equivilant to .then / .catch.
In this case you have mixed the syntaxes of both.
You don't need try / catch blocks when using .then,
just like you don't need to declare a function as async when using the try / catch syntax.

Other than that there's no reason they can't work together in seperate functions.

module.exports = (p) => {
  createPost(p).then(message => {
    console.log("ok " + message);
  }).catch(message => {
    console.log("failed " + message);
  });
};

createPost():

module.exports = async (p) => {
  try {
    const r = await getStat();
  } catch (error) {
    console.log(error);
  };
};

Upvotes: 2

Related Questions