Skip to content

Instantly share code, notes, and snippets.

@believer
Last active April 21, 2023 09:12
Show Gist options
  • Save believer/037e280d0f10c2704b91f47571d7391f to your computer and use it in GitHub Desktop.
Save believer/037e280d0f10c2704b91f47571d7391f to your computer and use it in GitHub Desktop.
const step = 100
const wait = 1000
const data = Array.from({ length: 69000 }, (_, i) => ({
id: i,
}))
const getData = async ({ id }) => Promise.resolve({ id, data: `data-${id}` })
const newData = []
const run = async (step) => {
const slicedData = data.slice(step - 100, step)
for (let d of slicedData) {
const updatedData = await getData(d)
newData.push(updatedData)
}
console.log(
`Items ${step - 100}/${step}. Have ${
newData.length
} items. Waiting for ${wait} ms...`
)
if (step >= data.length) {
console.log(newData.at(-1))
return
}
// setTimeout(() => run(step + 100), wait)
run(step + 100)
}
run(step)
// ------------------------------------------------------------------
// With chunks
// ------------------------------------------------------------------
const newData = []
// https://stackoverflow.com/questions/8495687/split-array-into-chunks
const chunk = (array, size) =>
array.reduce((acc, _, i) => {
if (i % size === 0) acc.push(array.slice(i, i + size))
return acc
}, [])
const run = async () => {
const chunks = chunk(data, step)
for (let chunk of chunks) {
for (let d of chunk) {
const updatedData = await getData(d)
newData.push(updatedData)
}
}
console.log(newData.at(-1))
}
run()
// ------------------------------------------------------------------
// With lodash chunk
// ------------------------------------------------------------------
import chunk from 'lodash.chunk'
const newData = []
const run = async () => {
for (let slicedData of chunk(data, 100)) {
for (let d of slicedData) {
const updatedData = await getData(d)
newData.push(updatedData)
}
}
console.log(newData.at(-1))
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment