Skip to content

Instantly share code, notes, and snippets.

@rnaffer
Last active October 27, 2015 17:22
Show Gist options
  • Save rnaffer/fedc313dad44573c6b29 to your computer and use it in GitHub Desktop.
Save rnaffer/fedc313dad44573c6b29 to your computer and use it in GitHub Desktop.
Uso de nuevas características de javascript (ES6) para hacer operaciones asíncronas de forma estructurada.
// El uso de generadores aún no está estandarizado. 25/10/2015
// Requiere generators.js
// El asterisco '*' lo define como generador
// yield detiene el procedimiento hasta que pueda continuar
// Librerias: bluebird para frontend -usado en el ejemplo-, co para bakcend node.js, q para angular
Promise.coroutine(function* () {
var user = yield $.get('user.json');
// Do stuff with user
var posts = yield $.get( 'posts.json?id=' + user.id );
// Do stuff with posts
var comments = yield $.get( 'comments.json?post_id=' + post.id );
// Do stuff with comments
})().catch(function( errs ) {
// Do stuff with errors
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment