Skip to content

Instantly share code, notes, and snippets.

@lmorchard
Created January 6, 2010 04:03
Show Gist options
  • Save lmorchard/270000 to your computer and use it in GitHub Desktop.
Save lmorchard/270000 to your computer and use it in GitHub Desktop.
var list = [ 1, 2, 3, 4, 5 ];
var loop_cb = (function () {
// if the list is empty, we're finished.
if (0 === list.length) { finished_cb(); }
// get the next value from the list
var next_value = list.shift();
// start a DB transaction
db.transaction(function (tx) {
// execute a SQL query
tx.executeSql(
"SELECT foo FROM bar WHERE baz=?", [ next_value ],
function (rs) {
// do something with the result set.
// ...
// perform the next loop iteration
loop_cb();
},
function () {
// something went wrong!
}
)
});
});
var finished_cb = (function () {
// all done with the above loop!
});
loop_cp(); // start the loop
// note that code past here continues to execute before the above loop has completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment