javascript - How to know if a function is async? -


i have pass function function, , execute callback. problem function async, like:

async function() {  // async actions } 

so want execute await callback() or callback() depending on type of function receiving.

is there way know type of function??

native async functions may identifiable when being converted strings:

asyncfn[symbol.tostringtag] === 'asyncfunction' 

or asyncfunction constructor:

const asyncfunction = (async () => {}).constructor;  asyncfn instanceof asyncfunction === true 

or make sure won't give false positives in transpiled code:

(asyncfn instanceof asyncfunction && asyncfunction !== function) === true 

the question refers babel implementation of async function, relies on transform-async-to-generator transpile async generator functions, may use transform-regenerator transpile generator normal functions.

the result of async function call promise. according proposal, promise or non-promise may passed await.

generally, async functions shouldn't distinguished regular functions return promises. , there no way or reason detect non-native async function in situation that.


Comments