I'm having trouble with a static async method on a javascript Class. If I remove the static keyword it works fine to call within the class but then I won't be able to call it by using the class.
My desired outcome is to use the exist method on the class itselt using User.exist(email) and on an instance of the class ex. foo.exist(email)
Where do I think wrong?
const userEmails = []
class User {
constructor(fields) {
this.email = fields.email;
this.name = fields.name;
}
static async exist(email) {
return setTimeout(function() {
return userEmails.includes(email)
}, 2000)
}
async storeEmail() {
let userExist = await this.exist(this.email)
if (userExist) {
console.log('User exist')
} else {
users.push(this.email)
console.log(userEmails)
}
}
};
let foo = new User({email: 'foo@bar.com', name: 'Foo Bar'})
foo.storeEmail() // this.exist is not a function
User.exist('foo@bar.com') // Works when used inside async function with await