I want to write an application that requests a token from an API. As long as this token isn't available I don't want to continue with the rest of the application. So it has to be like a synchronous HTTP request.
My goal is to create a function that does the request and then returns the token like:
var token=getToken(); //After this function has finished
makeRequest(token); //I want this function to be executed
How can I do this?
It doesn't want to be synchronous at all. Embrace the power of callbacks:
function getToken(callback) {
//get the token here
callback(token);
};
getToken(function(token){
makeRequest(token);
});
That ensures makeRequest isn't executed until after getToken is completed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With