Skip to content

Instantly share code, notes, and snippets.

@mekdev
Last active June 16, 2016 01:11
Show Gist options
  • Select an option

  • Save mekdev/fbb6764a21b7cb7538b1 to your computer and use it in GitHub Desktop.

Select an option

Save mekdev/fbb6764a21b7cb7538b1 to your computer and use it in GitHub Desktop.
Javascript asynchronous
var source = ['foo', 'bar', 'baz'];
var result = [];
setTimeout(function () {
for (var i = 0 ; i < source.length ; i++) {
console.log('Stepping through : ' + source[i]);
result.push(source[i]);
console.log('Current result: ' + result);
}
}, 1000); // Wait 1000 ms to finish operation
console.log('Result: ' + result);
console.log('Finished!!');
Result:
Finished!!
Stepping through : foo
Current result: foo
Stepping through : bar
Current result: foo,bar
Stepping through : baz
Current result: foo,bar,baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment