Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  8] [ 6]  / answers: 1 / hits: 34510  / 9 Years ago, thu, december 10, 2015, 12:00:00

Say I have a function object-



setObj : function(a,b){
obj.a = a;
obj.b = b;
}


If I have to use async & await on this function object, how do I do it?



If the same was written in function (function way), say-



async function setObj(a,b){
obj.a = a;
obj.b = b;
}

await setObj(2,3);


This works fine. But, how do I do it in case of function object?


More From » async-await

 Answers
92

If I understand your question correctly, you can just use the async keyword in front of the method declaration:



let obj = {};
let myObj = {
async setObj(a,b) {
obj.a = a;
obj.b = b;
}
}


See http://tc39.github.io/ecmascript-asyncawait/#async-methods



UPDATE



You cannot use await outside of an async function. In order to use this you have to wrap that call to await setObj(2, 3):



async function consoleLog() {
await myObj.setObj(2, 3);
console.log(obj.a + obj.b);
}

consoleLog();

[#64107] Tuesday, December 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brandensebastiand

Total Points: 323
Total Questions: 115
Total Answers: 106

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;