how to run async on class methods code example
Example 1: js class async constructor
function asyncRequest(id){
return new Paormise(/*.....*/);
}
class MyClass {
constructor(name, age, cityId) {
(async function() {
this.name = name;
this.age = age;
this.city = await asyncRequest(cityId);
})();
}
}
Example 2: async await js
fetch('coffee.jpg')
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
} else {
return response.blob();
}
})
.then(myBlob => {
let objectURL = URL.createObjectURL(myBlob);
let image = document.createElement('img');
image.src = objectURL;
document.body.appendChild(image);
})
.catch(e => {
console.log('There has been a problem with your fetch operation: ' + e.message);
});