I need to use a variable of a closure
as a function return to which this closure
is nested. How can I do this?
response
must be the return of ajaxRequest()
:
function ajaxRequest(type, url) {
const ajax = new XMLHttpRequest()
ajax.onreadystatechange = () => {
if(ajax.readyState === 4 && ajax.status === 200) {
let response = parseToJson(ajax.responseText) // → JSON.parse(str)
// ...
}
}
ajax.open(type, url)
ajax.send()
// return response
}
ajaxRequest
is called from two functions that contains the request data: tradesRequest()
and pricesRequest()
. I need the return of these two functions to call a third function, which will have as parameters the responses of the requests of the two functions mentioned above.
function tradesRequest() {
ajaxRequest(args) // args = type, url
}
function pricesRequest() {
ajaxRequest(args)
}
function display(trades, prices) {
// Esta função utilizará as respostas das funções acima.
}