'Why are js coroutines called generators?
It's pretty strange, for me. Generator is a function that has multiple entry points, can stop its execution and produce multiple results. Coroutine is the same, but it can not only produce data, but also consume it during the execution. So js generator is basically a coroutine. Why is it called a generator?
function* sumСalculator() {
const firstNum = yield "First num"
const secondNum = yield "Second num"
return firstNum + secondNum
}
const coroutine = sumСalculator()
console.log(coroutine.next().value)
console.log(coroutine.next(4).value)
console.log(coroutine.next(10).value)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
