'Javascript why Product2 is never called contrary to Product1
I'm experimenting with javascript function as object and async. Why Product2 below is never called ?
function Product(title) {
this.title = title;
this.getInfo = function() {
return this.title;
}
return this;
}
async function Product2(title) {
this.title = title;
this.getInfo = function() {
return this.title;
}
console.log(this.title)
return this;
}
async function main() {
console.log("start")
var product = new Product("car");
console.log(product.getInfo());
var product2 = await new Product2("car 2");
console.log(product2.getInfo());
console.log("end")
}
main();
Solution 1:[1]
async function Product2(title)
As this is async, There should be an uncaught TypeError when you are doing this:
var product2 = await new Product2("car 2");
cause Product2 is not a constructor anymore.
A function marked with async will return a promise.
A constructor on the other hand returns the object it is constructing. Thus we have a situation where you want to both return an object and a promise: an impossible situation.
Solution 2:[2]
new Product2 will create an instance of an object of product2
You cannot use await and new keyword together.
Solution 3:[3]
After being suggested https://github.com/cyrus01337/invites, I took a look and found a more elegant solution using async def on_invite_update(member, invite):
Thank you Lukasz in the comments for the suggestion.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shuvo |
| Solution 2 | |
| Solution 3 | Zak |
