在处理async
函数时,使用call()
或apply()
方法更改this
的语法是什么?
举个例子:
class Foo {
async someLongMethod(){
... Do stuff
}
}
class Bar {
async someOtherMethodThatWillWait(){
//Will the following work?
await Foo.prototype.someLongMethod.call(this);
... Do stuff but only after someLongMethod has completed
}
}
它与调用普通函数的语法完全相同,没有区别。您无法真正区分一个async function
和任何其他返回promise的function
。