is it possible to write asynchronous code in constructor javascript code example
Example 1: async constructor javascript
function asyncRequest(id){
return new Promise();
}
class MyClass {
constructor(async) {
(async function() {
this.city = await asyncRequest(async);
})();
}
}
Example 2: asynchronous function using function constructor
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
const func = new AsyncFunction('arg1', 'arg2', 'return arg1 * arg2 * await someAsyncCall();');
await func(2,2);
const func = new Function('arg1', 'arg2', 'return arg1 * arg2;');
func(2,2);