Skip to content

Instantly share code, notes, and snippets.

@wjvander
Last active April 4, 2018 10:37
Show Gist options
  • Select an option

  • Save wjvander/cffd450bfe6fa7dc4b89a9c1ddc2048c to your computer and use it in GitHub Desktop.

Select an option

Save wjvander/cffd450bfe6fa7dc4b89a9c1ddc2048c to your computer and use it in GitHub Desktop.
Javascript Cheat Sheet

Javascript Cheat Sheet

Arrays

Split Array into chunks

Usage

let someArray = [1, 2]
let chunkSize = 10
let chunks = chunk(someArray, chunkSize)

Code

const chunk = (a, l) => a.length === 0 ? [] : [a.slice(0, l)].concat(chunk(a.slice(l), l));

Async/Await batching of Array

Description

It runs batches synchronously, but async for each item in a batch

Code

let batches = chunk(myArray, 10)
for(let batch of batches){
  await Promise.all(batch.map(async(item) => {
    await doSomeAsyncStuffForEachItemInBatch(item);
  }));
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment