Reputation: 283
When i run the code below it logs out "test". However when i move the console.log statement inside the request function it logs the new value of splitBody. How do I make that newly set splitBody variable to be able to be referenced outside of the function and to retain its new value.
var splitBody = "test"
request('http://www.example.com', function (error, response, body) {
splitBody = body.split(" ");
}
console.log(splitBody)
Upvotes: 0
Views: 44
Reputation: 906
I am assuming you are sending an ajax request through you're request method. The function in the second parameter of request will be called once the response of the request is received. Javascript will not wait for the response and continue to execute the next lines of code. In this case it is the console.log method.
Upvotes: 1