Created
February 2, 2017 16:22
-
-
Save fidelisrafael/ea4c6d2139aa91d4742abc7ce306a79f to your computer and use it in GitHub Desktop.
Javascript Yield
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
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