0

I have a VPService.requestQueue which is an array of objects with the following content:

enter image description here

I need to do something after all promises have been completed so I'm using Promise.all(VPService.requestQueue).then(...) but its not working.

I tried making an array of both the canceller and promise and then calling Promise.all() on both of then both the console.logs inside this Promise.all() gets called BEFORE I receive the response from the promise.

        //getting an array of the promises from VPService.requestQueue
        var requestQueuePromises = VPService.requestQueue.map(function (queue) {
            return queue.canceller.promise;
        });
        //gettin array of cancellers
        var requestQueueCancellers = VPService.requestQueue.map(function (queue) {
            return queue.canceller;
        });

Now use Promise.all() to know when the promises have ended but both console.logs are being printed before my promises results are received.

        Promise.all(requestQueueCancellers).then(function () {
            console.log("After all cancellers")
        });
        Promise.all(requestQueuePromises).then(function () {
            console.log("After all promises")
        });
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
  • The cancelers look like Deferred objects, which not being thenable aren't valid for use with `Promise.all`. But you shouldn't need to do those anyway, if you're doing their promises. Can you put a [mcve] in the question, ideally using Stack Snippets (the `[<>]` toolbar button) to make it runnable? – T.J. Crowder Apr 13 '17 at 09:39
  • Does the `promise` object, under `canceller`, have `then` function ? – Oskar Apr 13 '17 at 09:40
  • @T.J.Crowder thanks for answer. Sorry but the code behind all this is quite long and complex. So is there any way to stop ongoing deferreds ? I tried `canceller.resolve()` but the first deferred us always executed if not is there any way to wait to do something until al deferreds are finished? – CommonSenseCode Apr 13 '17 at 09:42
  • @Oskar yes they do – CommonSenseCode Apr 13 '17 at 09:42
  • @CodingMcCodington: The point of a [mcve] is to trim the problem down to a reasonable size. See the link for details, but the point is: Usually you figure it out in the process, and if you don't, you have something contained you can ask for help with. – T.J. Crowder Apr 13 '17 at 09:47
  • @CodingMcCodington: Again: Why `Promise.all` on the cancelers when you're using `Promise.all` on their promises? – T.J. Crowder Apr 13 '17 at 09:48
  • @CodingMcCodington why not use Angular's promise with `$q`? – tanmay Apr 13 '17 at 09:50
  • @tanmay yup that is actually how the promises are being created: `var canceller = $q.defer(); factory.requestQueue.push({ url: config.api + 'timelinedata/' + params, canceller: canceller });` – CommonSenseCode Apr 13 '17 at 09:52
  • 1
    @CodingMcCodington so handle them with `$q.all(...)` – tanmay Apr 13 '17 at 09:53

0 Answers0