需求:
进程:
const uploadFile = async () => {
isCancel.current = false;
for (let i = 0; i < fileList.length; i++) {
if (isCancel.current) {
return;
}
const file = fileList[i]
const res = await uploadFiles({ file })
if (isCancel.current) {
break;
}
}
const createCanCancelRequest = (requestFunc: () => Promise<any>) => {
let func = () => {}
const promise = new Promise((resolve, reject) => {
func = () => reject("上传停止");
requestFunc().then(resolve).catch(reject)
})
return { cancel: func, promise }
}
const uploadFile = async () => {
isCancel.current = false;
for (let i = 0; i < fileList.length; i++) {
if (isCancel.current) {
return;
}
const file = fileList[i]
isBreak.current = false
const {cancel, promise} = createCanCancelRequest(
() => {
uploadFiles({ file });
isBreak.current = true
}
)
while(1) {
await new Promise(resolve => setTimeout(resolve, 1000));
if (isBreak.current) {
break
}
}
}
拓展: