Last active
February 26, 2016 16:34
-
-
Save jwerle/60fbc391123645ddc0ee to your computer and use it in GitHub Desktop.
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
'use strict' | |
let co = require('co') | |
function * list (array, subdivision) { | |
let deferred = Promise.defer() | |
let marker = 0; | |
let stop = marker + subdivision | |
let max = A.length | |
let to = Math.random() * 1000 | 0 | |
while (marker < max) { | |
setTimeout(() => { | |
let sliced = array.slice(marker, stop) | |
marker += subdivision | |
stop = marker + subdivision | |
deferred.resolve(sliced) | |
deferred = Promise.defer() | |
}, to) | |
yield deferred.promise | |
} | |
} | |
co(function * () { | |
let array = new Array(16).fill(1).map((_, i) => i + 1) | |
let it = list(array, 4) | |
let a = yield it.next() | |
let b = yield it.next() | |
let c = yield it.next() | |
let d = yield it.next() | |
console.log(a, b, c, d) | |
}) | |
.then(console.log) | |
.catch(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment