Example of custom implementation 'range' function using generator and ES2015 syntax.
A Pen by Vlad Bezden on CodePen.
Example of custom implementation 'range' function using generator and ES2015 syntax.
A Pen by Vlad Bezden on CodePen.
'use strict'; | |
let range = function*(start, end) { | |
let current = start; | |
while (current <= end) { | |
let delta = yield current; | |
current += delta || 1; | |
} | |
}; | |
let data = range(1, 10); | |
console.log(Array.from(data)); | |
let data2 = []; | |
let iterator = range(1, 10); | |
let next = iterator.next(); | |
while (!next.done) { | |
data2.push(next.value); | |
next = iterator.next(2); | |
} | |
console.log(data2); |