Skip to content

Instantly share code, notes, and snippets.

@fidelisrafael
Created February 2, 2017 16:22
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 fidelisrafael/ea4c6d2139aa91d4742abc7ce306a79f to your computer and use it in GitHub Desktop.
Save fidelisrafael/ea4c6d2139aa91d4742abc7ce306a79f to your computer and use it in GitHub Desktop.
Javascript Yield
function* process() {
console.log('Start process 1');
console.log('Pause process2 until call next()');
yield;
console.log('Resumed process2');
console.log('Pause process3 until call next()');
yield;
console.log('Resumed process3');
console.log('End of the process function');
}
let _process = process();
console.log(_process.next())
console.log("A função está pausada no primeiro `yield`")
console.log(_process.next())
console.log("A função está pausada no segundo `yield`")
console.log(_process.next())
console.log("A função foi totalmente executada")
console.log(_process.next()) // não executa mais função
console.log(_process.next()) // não executa mais a função
function* infiniteNext() {
// this will yield the expression 999 times
for(let i=0;i < 999; i++) {
yield;
}
}
let i = infiniteNext();
// 995 calls to .next() this will execute function body
for(let l=0;l < 995; l++) { i.next() }
console.log(i.next()) // { done: false }
console.log(i.next()) // { done: false }
console.log(i.next()) // { done: false }
console.log(i.next()) // { done: false }
console.log(i.next()) // { done: true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment