ujc
10/24/2016 - 8:53 PM

[blog] wait sync example 1

[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)       //
////////////////////////////////////////////////////////