Skip to content

Instantly share code, notes, and snippets.

@pH200
Last active December 23, 2015 23:59
Show Gist options
  • Save pH200/6713317 to your computer and use it in GitHub Desktop.
Save pH200/6713317 to your computer and use it in GitHub Desktop.
q parallel example 2
var Q = require( 'q' );
var overwrite_array = function ( source, array ){
var result = source.slice( 0 );
Array.prototype.splice.apply( result, [ 0, array.length ].concat( array ));
return result;
};
var xyz = [ 5, 6, 7 ];
Q.all([
// simulates a time consuming io operation
Q.delay( 200 ).then( Q.fbind( function ( xyz ){
console.log( 'first task ---------------' );
console.log( 'x : ' + xyz[ 0 ] );
console.log( 'y : ' + xyz[ 1 ] );
console.log( 'z : ' + xyz[ 2 ] + '\n' );
return [ 11, 12 ];
}, overwrite_array( xyz, [ 7 ]))),
Q.delay( 100 ).then( Q.fbind( function ( xyz ){
console.log( 'second task ---------------' );
console.log( 'x : ' + xyz[ 0 ] );
console.log( 'y : ' + xyz[ 1 ] );
console.log( 'z : ' + xyz[ 2 ] + '\n' );
return 1000;
}, overwrite_array( xyz, [ 9, 10, 55 ])))
]).then(function ( from_parallel ){
return Q.delay( 150 ).then( Q.fbind( function ( xyz ){
console.log( 'all finished callback ---------------' );
console.log( 'from_parallel : ' + JSON.stringify( from_parallel ));
console.log( 'x : ' + xyz[ 0 ] );
console.log( 'y : ' + xyz[ 1 ] );
console.log( 'z : ' + xyz[ 2 ] + '\n' );
}, overwrite_array( xyz, [ 700 ])));
}).done();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment