I have the following method that I'm trying to complete:
getAllValues: function (callback) {
this.getCount((count) => { // count is usually 5
let results = []
for (var i = 0; i < count; i++) {
this.getValue(i, (result) => { // getValue() is async and eventually returns a string
results.push(result)
})
if (i == count-1) {
callback(results)
}
}
I want results
to be an array with all of the strings returned by getValue()
; however, I haven't been able to figure out how to do this. In callback(results)
, results
ends up being an empty array, so the pushed values are being dropped somehow
How do I get this to do what I want?
EDIT: I do NOT want to use promises here.