How to know if a function is async?

后端 未结 8 811
别跟我提以往
别跟我提以往 2020-12-01 00:55

I have to pass a function to another function, and execute it as a callback. The problem is that sometimes this function is async, like:

async function() {
          


        
8条回答
  •  天涯浪人
    2020-12-01 01:45

    It seems that await can be used for normal functions too. I'm not sure if it can be considered "good practice" but here it is:

    async function asyncFn() {
      // await for some async stuff
      return 'hello from asyncFn' 
    }
    
    function syncFn() {
      return 'hello from syncFn'
    }
    
    async function run() {
      console.log(await asyncFn()) // 'hello from asyncFn'
      console.log(await syncFn()) // 'hello from syncFn'
    }
    
    run()
    

提交回复
热议问题