-1

I am missing something fundamental in terms of callbacks/async in the code below: why do I get:

[,,'[ {JSON1} ]']

[,,'[ {JSON2} ]']

(=2 console returns) instead of only one console return with one proper table, which is want I want and would look like:

[,'[ {JSON1} ]','[ {JSON2} ]']

or ideally:

[{JSON1},{JSON2}]

See my code below, getPTdata is a function I created to retrieve some JSON via a REST API (https request). I cannot get everything at once since the API I'm talking to has a limit, hence the limit and offset parameters of my calls.

offsets = [0,1]
res = []

function goGetData(callback) {
    for(var a = 0; a < offsets.length; a++){
        getPTdata('stories',
                  '?limit=1&offset='+offsets[a]+'&date_format=millis',
                  function(result){
            //called once getPTdata is done
            res[a] = result
            callback(res)
        });
    }
}

goGetData(function(notgoingtowork){
    //called once goGetData is done
    console.log(res)
})
Night
  • 85
  • 1
  • 2
  • 7
  • because.. you're calling the callback once each time one of them completes, rather than once when both are complete. You're also having a for loop closure problem with the `a` variable. – Kevin B Sep 06 '16 at 14:41
  • http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example/750506?s=4|0.0000#750506 and http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323?s=1|0.0000#14220323 – Kevin B Sep 06 '16 at 14:43
  • ok, modified to: `if (a = offsets.length-1) { callback(res) }` but I don't understand the other issue: how come I still get 2 sets of results? thanks so much for helping, this saves my day! – Night Sep 06 '16 at 15:04
  • aren't you wanting two sets of results? `[setone,settwo]`? – Kevin B Sep 06 '16 at 15:05
  • Did you implement the first link i provided? – Kevin B Sep 06 '16 at 15:10
  • I'm still trying to understand it. I just realized I used = instead of == in the correction above so... with `if (a == offsets.length-1) { callback(res) }` I think I'm solving part of the problem, the problem with the a for loop closure I don't know how to solve, I can't seem to understand why, now, `a` will never be equal to 1... – Night Sep 06 '16 at 15:16
  • it's actually never equal to 0, it's always 1 by the time the callbacks happen. that is explained in the first link. – Kevin B Sep 06 '16 at 15:18
  • I am lost... Trying this but doesnt work, `function createcall(offset) { return function() { getPTdata('stories','?limit=1&offset='+offset+'&date_format=millis', function(result){ return(result) }) } } function goGetData(callback) { for(var a = 0; a < offsets.length; a++){ createcall(a, function(){ //called once getPTdata is done, therefore we know result and can store it res[a] = result if (a == offsets.length-1) { callback(res) } }); } }` – Night Sep 06 '16 at 15:34

1 Answers1

0

Solved like this:

offsets = [0,1]
res = []

function goGetData(callback) {

    var nb_returns = 0
    for(var a = 0; a < offsets.length; a++){
        getPTdata('stories','?limit=1&offset='+offsets[a]+'&date_format=millis', function(result){
            //note because of "loop closure" I cannot use a here anymore 
            //called once getPTdata is done, therefore we know result and can store it
            nb_returns++
            res.push(JSON.parse(result))
            if (nb_returns == offsets.length) {
                callback(res)
            }
        });
    }
}


goGetData(function(consolidated){
    //called once goGetData is done
    console.log(consolidated)
})
Night
  • 85
  • 1
  • 2
  • 7