Skip to content

Instantly share code, notes, and snippets.

@matthieu-D
Last active November 4, 2016 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthieu-D/b56647e6a896f32fec0aee660dffbac4 to your computer and use it in GitHub Desktop.
Save matthieu-D/b56647e6a896f32fec0aee660dffbac4 to your computer and use it in GitHub Desktop.
function getIdInDb () {
// Simulate async Db request
setTimeout(function() {
tmp.next(2); // I return 2 as a result for my Id
}, 2000);
}
// Use the id returned by the yielded function
function getUserWithId (id) {
console.log('Getting user with id:', id);
}
function *doSomethingInTheDb () {
let id = yield getIdInDb();
console.log('The id is:', id);
getUserWithId(id);
}
// Stock the generator
var tmp = doSomethingInTheDb();
// Trigger the first yield
tmp.next();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment