javascript异步
JavaScript async/await has changed the landscape of how we code. We're no longer stuck in callback or then
hell, and our code can feel more "top down" again.
JavaScript异步/等待改变了我们编码方式的面貌。 我们不再停留在回调或then
地狱,我们的代码可以感受到更多的“自上而下”的一次。
Async functions require the following syntax:
异步函数需要以下语法:
async function myFunction() {
}
To use await
with a function, the function needs to be declared with async
. That got me to thinking: is it possible to detect if a function is asynchronous?
要将await
与函数一起使用,必须使用async
声明函数。 这让我开始思考:是否可以检测一个函数是否异步?
To detect if a function is asynchronous, use the function's constructor.name
property:
要检测函数是否异步,请使用函数的constructor.name
属性:
const isAsync = myFunction.constructor.name === "AsyncFunction";
If the value is AsyncFunction
, you know the function is async
!
如果该值为AsyncFunction
,则AsyncFunction
该函数为async
!
Async functions are my preferred method of working with promises. Knowing if a function is async could be useful as a library creator or a typing/validation utility.
异步函数是我处理诺言的首选方法。 知道某个函数是否异步对于作为库创建者或键入/验证实用程序很有用。
翻译自: https://davidwalsh.name/javascript-detect-async-function
javascript异步