Created
May 17, 2020 13:18
-
-
Save hoodwink73/c046350fc689c2fd0b65016842f38086 to your computer and use it in GitHub Desktop.
Batch and iterate async tasks
This file contains hidden or 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
import ASQ from 'asynquence'; | |
import "asynquence-contrib" | |
import fetch from 'isomorphic-unfetch'; | |
var fetcher = (url) => | |
ASQ(url) | |
.then((done) => | |
fetch(url) | |
.then(res => res.json()) | |
.then((data) => done(data)) | |
) | |
// start a timer | |
// count to 1 sec | |
// limit only 10 reqs in that one sec | |
// if 10 reqs are over wait for a second to start again | |
var todosSeq = fetcher("https://jsonplaceholder.typicode.com/todos/1") | |
var getTodo = (id) => fetcher(`https://jsonplaceholder.typicode.com/todos/${id}`) | |
// ASQ(21) | |
// .then(todosSeq) | |
// .then((done, data) => { | |
// data | |
// }) | |
// is tapping a sequence means executing the steps that | |
// are available at the time of tapping it (ASQ().then(seq)) | |
// todosSeq.then((done, data) => { | |
// fetcher(`https://jsonplaceholder.typicode.com/users/${data.userId}`) | |
// .then((done, data) => { | |
// data | |
// }) | |
// }) | |
// todosSeq.then((done, data) => { | |
// fetcher(`https://jsonplaceholder.typicode.com/users/${data.userId}`) | |
// .pipe(done) | |
// }).val((val) => { | |
// val //? | |
// }) | |
var sucesses = []; | |
var failures = []; | |
var batch = (tasks, batchSize) => { | |
var batches = []; | |
for (let i =0; i < tasks.length; i+= batchSize) { | |
batches.push(tasks.slice(i, batchSize)) | |
} | |
return batches | |
} | |
function main () { | |
var batchedTasks = batch([...Array(100)].map((_, index) => () => getTodo(index + 1)), 10) | |
ASQ() | |
.runner(function *step(token){ | |
var x = 0; | |
var todos = []; | |
while (x < 1) { | |
yield ASQ().after(1000).any(...batchedTasks[x].map(task => task())) | |
.val((...newTodos) => { | |
todos.push(...newTodos) | |
console.log(`${x} complete`, newTodos) | |
}) | |
x += 1; | |
} | |
return todos | |
}) | |
.val(function(todos){ | |
console.log("complete"); | |
console.log(todos, todos.length); | |
}); | |
} | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment