Skip to content

Instantly share code, notes, and snippets.

@Ivanca
Last active December 10, 2015 14:08
Show Gist options
  • Select an option

  • Save Ivanca/4445537 to your computer and use it in GitHub Desktop.

Select an option

Save Ivanca/4445537 to your computer and use it in GitHub Desktop.
/**************
* Con promesas
**************/
var promise = foo().then(function(){
var promise = new Promise();
asyncFunction(function(){
// It has to know there is no need to merge it to a received object in order to
// do this;
var data = {key1:0x111};
promise.done(data);
})
return promise;
}).then(function(data){
var promise = new Promise();
asyncFunction(function(){
// It has to know the previous
// result was an OBJECT in order to do this
data.key2 = 0x222;
promise.done(data);
})
return promise;
},function(err, data, promise){
if(err) data.error = true;
promise.done(data);
}).parallel(function(data){
var promise = new Promise();
asyncFunction(function(){
data.key2 += 0x333;
promise.done(data);
});
return promise;
}).parallel(function(data){
var promise.done(data);
asyncFunction(function(){
data.key2 += 0x444;
promise.done(data);
})
return promise;
});
promise.then(function(){
console.log("Por Fin!");
});
/**************
* Sin promesas
**************/
foo(function(){
var data = {};
asyncFunction(function(){
data.key1 = 0x111;
asyncFunction(function(err){
if(err){
data.error = true;
return;
}
data.key2 = 0x222;
parallel();
});
});
var parallel = function(){
var count = 0;
asyncFunction(function(){
data.key2 += 0x333;
end();
});
asyncFunction(function(){
data.key2 += 0x444;
end();
});
var end = function(){
if(++count === 2)
console.log("Fin")
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment