Skip to content

Instantly share code, notes, and snippets.

@jwerle
Last active February 26, 2016 16:34
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 jwerle/60fbc391123645ddc0ee to your computer and use it in GitHub Desktop.
Save jwerle/60fbc391123645ddc0ee to your computer and use it in GitHub Desktop.
'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