Last active
October 27, 2015 03:40
-
-
Save rnaffer/3d0967c1755b0425d2c2 to your computer and use it in GitHub Desktop.
Concatenar operaciones asíncronas empleado promesas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Para los casos en los que es necesario concatenar tareas asíncronas. | |
// $.get() es una version simplificada de esto: | |
$.ajax({ | |
url: url, | |
data: data, | |
success: success, | |
dataType: dataType | |
}); | |
// Cada operacion al finalizar retorna una promesa que es empleada como una cadena de tareas | |
$.get('user.json') | |
.then(function( user ) { | |
// Do stuff with user | |
return $.get( 'posts.json?id=' + user.id ); | |
}).then(function( posts ) { | |
// Do stuff with posts | |
return $.get( 'comments.json?post_id=' + post.id ); | |
}).then(function( comments ) { | |
// Do stuff with comments | |
}, handleError); | |
function handleError( xhr, status, error ) { | |
// Do stuff with errors | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment