Код: Выделить всё
let url = "http://localhost:3000/api/v1/test";
let chunkSize = 100;
const chunks = data.reduce((acc, _, i) => {
if (i % chunkSize === 0) acc.push([]);
acc[acc.length - 1].push(array[i]);
return acc;
}, [] as T[][]);
console.log('chunks',chunks);
for (const chunk of chunks) {
console.log( 'chunk',chunk.length);
await fetch( url , {
method: 'POST',
body: JSON.stringify( chunk ) ,
"Content-type": "application/json; charset=UTF-8"
}
});
await setTimeout(function(){},1000);
console.log( 'chunk',chunk.length);
< /code>
Однако при тестировании процесс застрял при отправке первой части, нет ошибки, просто застряла. < /p>
Второй путь: < /p>
let url = "http://localhost:3000/api/v1/test";
let chunkSize = 100;
const chunks = data.reduce((acc, _, i) => {
if (i % chunkSize === 0) acc.push([]);
acc[acc.length - 1].push(array[i]);
return acc;
}, [] as T[][]);
return Promise.all(chunks.map(async chunk => {
console.log( 'chunk',chunk.length);
await setTimeout(function(){},1000); // not working at all
return await fetch( url , {
method: 'POST',
body: JSON.stringify( chunk ) ,
headers: {
"Content-type": "application/json; charset=UTF-8"
}
});
}));
Подробнее здесь: https://stackoverflow.com/questions/797 ... with-sleep