javascript异步_JavaScript检测异步功能

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异步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值