Skip to content

Instantly share code, notes, and snippets.

@dbolser
Last active April 29, 2019 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbolser/d530d3d54368a1a3d1b3b587e688d7f5 to your computer and use it in GitHub Desktop.
Save dbolser/d530d3d54368a1a3d1b3b587e688d7f5 to your computer and use it in GitHub Desktop.
Should I be writing JS like this?
// A variable to know when to exit the 'inner' function. When entered = data.length, we know we can safely continue.
var entered = 0
var mah_list = []
async function inner(d, dlength){
// SQL2
setTimeout(function(){
console.log('inner', d, dlength)
mah_list.push(d)
entered++;
if (entered==dlength) {
console.log("All done");
final(mah_list);
}
}, 500, d)
}
async function outer(){
// SQL1
var data = [1, 2, 3, 4]
for(d in data) {
console.log('outer', d, entered)
inner(d, data.length)
}
}
outer()
console.log('here already?')
final = function (some_list) {
console.log('continuing')
console.log(some_list)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment