Skip to content

Instantly share code, notes, and snippets.

@uri-chandler
Created October 24, 2016 20:53
Show Gist options
  • Save uri-chandler/f4609945e8f6ae1f08f969d9c7904566 to your computer and use it in GitHub Desktop.
Save uri-chandler/f4609945e8f6ae1f08f969d9c7904566 to your computer and use it in GitHub Desktop.
[blog] wait sync example 1
// using wait-sync, you can use both async *and* sync code in the same execution block
// without worring about blocking the thread (so node's event-loop keeps running, I/O still works as expected etc..)
var waitSync = require('wait-sync');
// the easy part - just log "1" to the console
console.log(1);
// next, setup some async operation that will log "2" sometime in the future - say, 3 seconds from now
setTimeout(() => console.log(2), 3000);
// magic-time! let's use wait-sync to wait out those 3 seconds before allowing this execution block to continue
waitSync(3);
// last, let's log "3" for completeness
console.log(3)
////////////////////////////////////////////////////////
// result: 1 2 3 (as expected, and not 1 *3* 2) //
////////////////////////////////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment