EJEMPLO DE ORDENAMIENTO DE CALL BACKS EN NODE JS CON LA LIBRERIA ASYNC
var async = require('async');
async.waterfall([
function (cb){
artist.findEvents('Miley', cb);
},
function (concert, cb){
concert.findTickets({price: {$lt: 100}}, cb);
},
function (tickets, cb){
tickets.purchase({cc: 424242424242}, cb);
},
function (transaction, cb) {
email.confirm(customer, transaction, cb);
}
],
//FUNCTION RETORNO
function (null, status){
if(err){
res.send(err);
return false;
}
res.send({
tickets: 'purchased'
});
}
);//FIN WATERFALL
//PATRON PROMISES DE COMMONJS - promises-aplus.github.io/promises-spec/
artist.find('Miley')
.findEvents({city: 'Bogota'})
.findTickets({price: {$lt: 100}}, cb);
.purchase({cc: 424242424242}, cb);
.then (function(transaction) {
customer.transaction = transaction;
return customer.save();
})
.then (function (customer, transaction) {
return email.confirm(customer, transaction);
})
.then function(){
res.send({
tickets: 'purchased'
});
},
function (err) {
console.log('Ha habido un error', err);
}
);
EJEMPLO DE CALL BACKS DESORDENADOS
function startExecution() {
doSomethingAsync(function(error, result) {
doSomethingElseAsync(function(error, result) {
moreAsync(function(error, result) {
evenMoreAsync(function(error, result) {
// ... I think you got it
});
});
});
});
};
0 comentarios:
Publicar un comentario