Making `while` loop in TypeScript upload concurrent
Hi, is there way to use this while loop like
Effect.foreachEffect.foreach to make it run concurrent ?class UploadError extends Data.TaggedError("UploadError")<{ msg: string }> {}
async function uploadVideo() {
// 5 megabytes
const CHUNK_SIZE = 5 * 1024 * 1024;
const file = form.video.files[0];
const fileSize = file.size;
let start = 0;
let end = Math.min(CHUNK_SIZE, fileSize);
let chunkNumber = 0;
while (start < fileSize) {
await Effect.tryPromise({
try: async () => {
const chunk = file.slice(start, end);
const formData = new FormData();
formData.append("video", chunk, `${chunkNumber}_${file.name}`);
const res = await axios.post("/", formData, {
headers: {
"Content-Type": "multipart/form-data",
},
onUploadProgress: (event) => {
per = (((start + event.loaded) / fileSize) * 100).toFixed(0);
console.log(event);
},
});
if (res.status === 200) {
per = ((end / fileSize) * 100).toFixed(0);
start = end;
end = Math.min(start + CHUNK_SIZE, fileSize);
chunkNumber++;
} else {
throw new Error("Failed to upload video");
}
},
catch: (err: any) => new UploadError({ msg: err.message }),
}).pipe(
Effect.timeoutFail({
duration: "5 seconds",
onTimeout: () => new UploadError({ msg: "Upload timed out" }),
}),
Effect.tapError((err) => Console.log(err._tag, err.msg)),
Effect.eventually,
Effect.runPromise,
);
}
if (start >= fileSize) {
per = "ok";
}
}class UploadError extends Data.TaggedError("UploadError")<{ msg: string }> {}
async function uploadVideo() {
// 5 megabytes
const CHUNK_SIZE = 5 * 1024 * 1024;
const file = form.video.files[0];
const fileSize = file.size;
let start = 0;
let end = Math.min(CHUNK_SIZE, fileSize);
let chunkNumber = 0;
while (start < fileSize) {
await Effect.tryPromise({
try: async () => {
const chunk = file.slice(start, end);
const formData = new FormData();
formData.append("video", chunk, `${chunkNumber}_${file.name}`);
const res = await axios.post("/", formData, {
headers: {
"Content-Type": "multipart/form-data",
},
onUploadProgress: (event) => {
per = (((start + event.loaded) / fileSize) * 100).toFixed(0);
console.log(event);
},
});
if (res.status === 200) {
per = ((end / fileSize) * 100).toFixed(0);
start = end;
end = Math.min(start + CHUNK_SIZE, fileSize);
chunkNumber++;
} else {
throw new Error("Failed to upload video");
}
},
catch: (err: any) => new UploadError({ msg: err.message }),
}).pipe(
Effect.timeoutFail({
duration: "5 seconds",
onTimeout: () => new UploadError({ msg: "Upload timed out" }),
}),
Effect.tapError((err) => Console.log(err._tag, err.msg)),
Effect.eventually,
Effect.runPromise,
);
}
if (start >= fileSize) {
per = "ok";
}
}