Skip to content Skip to sidebar Skip to footer

Async Function Returns [object Promise]

I'm trying to return an async function but I either get promise: < { PENDING } > or [object Promise] instead of [object Object] I've tried returning the value using Promise.r

Solution 1:

You cannot make asynchronous code synchronous.

The await keyword lets you write code that looks synchronous, but is really asynchronous.

You can only use await inside a function which is async.

To make await work, allasync functions return promises. Those promises resolve to be the return value of the function.

In short: You have the normal and expected behaviour, and you can't prevent an async function from returning a promise.

Solution 2:

I solved this by adding an await to the top level getNext() aka

const next = await getNext({
  page,
  value,
  name,
  id,
});

which is inside an async router.post call. So the answer is: make sure all functions are async all the way to the top so that they wait for eachother.

Post a Comment for "Async Function Returns [object Promise]"