Using async requires async function, but my function is async

前端 未结 4 771
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 07:44

I\'m adapting a library that uses callback to use Promises. It\'s working when I use then(), but it doesn\'t work when I use await.



        
4条回答
  •  逝去的感伤
    2021-01-18 08:41

    SyntaxError: await is only valid in async function - just like the error tells you, you may only use await inside a function which is marked as async. So you cannot use the await keyword anywhere else.

    https://basarat.gitbooks.io/typescript/docs/async-await.html

    https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-7.html

    examples:

    function test() {
      await myOtherFunction() // NOT working
    }
    
    async function test() {
      await myOtherFunction() //working
    }
    

    You can also make anonymous callback functions async:

    myMethod().then(async () => {
      await myAsyncCall()
    })
    

提交回复
热议问题