For anyone who uses callbacks, how do I approach it when the method is an asynchronous setter?
Here's an example
class API
{
constructor()
{
this.token = null;
}
refreshToken(callback)
{
request("http://api.com/token", (token) => {
this.token = token;
callback(token);
});
}
}
What would be the point of returning the token if I've already set it in the object's property?
I find myself dumbfounded when I end up doing something like this:
api.refreshToken((token) => {
//callback is used as a 'wait until token arrives' for proceeding code
request("http://api.com/data?token=" + token);
});
But I need it as an object property so that I can reuse the token, such as:
request("http://api.com/data?token=" + this.token);
But now I have two variables for the same thing, provided by OOP's property and the callback. It's a pretty stupid problem, but I need direction.