【发布时间】:2019-01-07 13:58:33
【问题描述】:
使用 call 调用或使用 await 应用的函数永远无法解析,请检查以下代码 sn-p
const obj = {
resolveAfter2Seconds: function() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
}
async function asyncCall() {
console.log('calling');
var result = await obj.resolveAfter2Seconds();
console.log(result);//resolved
var result2 = await obj.call("resolveAfter2Seconds");
console.log(result2);//never alled
}
asyncCall();
【问题讨论】:
-
obj不是函数。obj.call毫无意义。Uncaught (in promise) TypeError: obj.call is not a function -
通过 call(),一个对象可以使用属于另一个对象的方法。因为只有一个全局对象。无需致电/申请。
-
我明白了,我已经在另一个答案中澄清了......谢谢