0

I'm going to put a barebones example here of what I think should work, but doesn't.

const getData = async () => {
    const data = await makeAnAsyncCall()    //returns a promise of SomeType
    return data
}

const actuallyUseTheData = () => {
    const data: SomeType = getData()
    //more code...
}

This invariably leads to the error:

Variable of type Promise<SomeType> cannot be assigned to SomeType. Did you forget to use 'await'?

But I'm getting the data in an async function which uses await!

5
  • You forgot to await the call to getData(). (Which means actuallyUseTheData would also need to be async, unless you want/need to use callbacks therein for some other reason.) Commented Oct 28 at 17:58
  • It's turtles all the way down (got to use async/await on all parents of the child) ((unless special cases when you don't)) Commented Oct 28 at 17:59
  • 1
    "I've never found an explanation that helps me avoid this." Do you have links to questions/answers on SO that don't explain it sufficiently for you? Otherwise there's a strong chance this will be closed as a duplicate of one of those. Commented Oct 28 at 18:01
  • Like, stackoverflow.com/questions/35380162/… is the one I'd start to look at. Basically "async (or Promises, etc) is contagious" is the issue. await doesn't turn asynchronous code into synchronous code, it just turns asynchronous code into code that looks synchronous. You can't synchronously call an async function and get the answer "right away". Commented Oct 28 at 18:04
  • 1
    @GeneralGrievance I've edited the list of duplicate targets to the canonical ones we have for this topic Commented Oct 28 at 18:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.