Consider this:
class ConnectUser {
connect = () => {
axios.get('URL').catch(err => {
// CAN I CALL `connect` METHOD AGAIN?!
// this.connect();
});
}
}
My code has a method and can connect or refuse to connect to some resources. If an exception happens can I call it again to strive for connecting?
As an alternative to Danielo's answer - if you want this
inside connect
to refer to the class instance, you can simply define connect
as a method of the class, ie
class ConnectUser {
connect() {
axios.get('URL').catch(err => {
return this.connect();
});
}
}
Yes, you can. But if this is something you want to generalize on your app, consider using an axios plugin that automatically retries and only fails after the amount of retries you specify. You can call just connect function again if you define it as a separate function on the scope instead of a class method. But if you really need to call using this, save the proper this reference in the outer closure like this:
connect = () => {
const self = this
axios.get('URL').catch(err => {
self.connect();
});
}
Then use self instead
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.