3

I'm implementing big file upload task. I would split a big file into hundreds promises tasks.

Promise.all([100_subtasks]).then(DO_SOMETHING)

However, Let's say there's a subtask failed or a user wants to abort the entire process manually.

How do I do?

My idea is that every subtask will do a special check on a flag.

    subTask(){
        // before do a upload
        if (cancelFlag === true){
            abort the following upload process
        }

    }

    userClicksCancel(){
        // turn the flag to true
        cancelFlag = true ;
    }

Thanks a lot!

newBike
  • 14,385
  • 29
  • 109
  • 192
  • 1
    I've had luck with [`p-cancelable`](https://github.com/sindresorhus/p-cancelable) - or you could [peek under the hood](https://github.com/sindresorhus/p-cancelable/blob/master/index.js) to see how it is being achieved there. – Alexander Nied Oct 24 '19 at 04:40
  • Unfortunately, there is no baked-in solution for canceling a promise yet. [This post](https://stackoverflow.com/questions/30233302/promise-is-it-possible-to-force-cancel-a-promise) has some ways to implement that behaviour on your own. – 31piy Oct 24 '19 at 04:46
  • If a subtask fails, it should just reject and `Promise.all()` will immediately reject. That's the behavior of `Promise.all()`. The other subtasks will eventually finish on their own, but they won't be part of the `Promise.all()` result any more. `Promise.all()` should be used when all your asynchronous operations are running in parallel and thus the outcome of one should not be impacting the others. If that's the case, then perhaps `Promise.all()` is not the right structure. – jfriend00 Oct 24 '19 at 05:05
  • @jfriend00 what do u recommend for the **cancel** case, a user can abort all promises from outside. any pattern recommend? – newBike Oct 24 '19 at 05:11
  • What do you mean "user can abort all promises". If they are really getting aborted, they should just reject and `Promise.all()` will pick that up for you. Or, if you mean "user would like to cancel all asynchronous operations", then we'd have to see the actual asynchronous operations as there's no generic cancel. How to abort a given asynchronous operation depends upon the specific operation. For example, a timer is stopped with `clearTimeout()`. Some other types of asynchronous operations in node.js are not cancellable. – jfriend00 Oct 24 '19 at 05:12
  • *abort the following upload process* ... you mean *don't start the following upload process* ... because if the process follows the code you posted, then you aren't aborting anything, because it hasn't started yet, as it is *following* – Bravo Oct 24 '19 at 05:17
  • Depends on exactly what you want not to happen. There's a difference between preventing side-effects in each of the subtasks versus preventing `DO_SOMETHING` firing regardless of subtask outcomes. Whichever, rather than use a flag, you might consider racing your promises (individually or en masse) against a Promise that will, or can be made to reject. – Roamer-1888 Oct 24 '19 at 07:33
  • [The answers here](https://stackoverflow.com/q/30233302/3478010) shuold make interesting reading. – Roamer-1888 Oct 24 '19 at 07:38
  • Depends also on support requirements. You are basically looking for [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). It also allows to abort multiple fetches at once. Then you can call abort() on reject other than AbortError and btn click. – bigless Oct 25 '19 at 23:19

0 Answers0