Functions that are called with call or apply using await never resolve, please check the following code snippet
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();
Just expanding @CertainPermances comment - call is a function method used to assign an option to this inside the function, you can't use it on an object
(function() {
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"); caused an eror
var result2 = await obj.resolveAfter2Seconds();
// or
var result2 = await obj.resolveAfter2Seconds.call( obj /* perhaps */); // which makes no difference at all because this = obj anyway
console.log(result2);
}
asyncCall();
})();
I was asking the question in wrong way, I ended up using the correct syntax as follows
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["resolveAfter2Seconds"].call();
console.log(result2);//never alled
}
asyncCall();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.