Opened 6 years ago

Closed 3 years ago

#17532 closed defect (patchwelcome)

dojo/promise/all should allow some resolved & some rejected with always()

Reported by: Arlo White Owned by:
Priority: undecided Milestone: 1.13
Component: Core Version: 1.9.1
Keywords: Cc:
Blocked By: Blocking:

Description

I have a situation where I'm merging results from two requests and if one fails I want to be able to continue. I'd expect always() to allow something like this, but it does not provide an array, instead I just get a single error if one Promise is rejected.

I want to do something like this:

all([request1, request2]).always(function(results){
 var result1 = results[0];
 if(!result1){
  throw new Error('This data is required.');
 }
 var result2 = results[2];
 if(result2){
   // Resolved, use the data...
 }
});

Change History (4)

comment:1 Changed 6 years ago by Arlo White

This is my workaround, which really isn't so bad. Still, this would be a nice feature.

all([enrollmentRequest, portlet.moodleRequest])
     .then(lang.hitch(self, 'handleEnrollment'), function(){
         console.debug('Failed to load enrollment history & Moodle data',arguments);
         // Try enrollment history alone.
         enrollmentRequest.then(function(data){
                 console.debug('Calling handleEnrollment with enrollment data only');
                 self.handleEnrollment([data, null]);
         });
     });

comment:2 Changed 6 years ago by freddefisk

In your example, how would the callback function passed to "always" detect if a promise was resolved or rejected? Or would you only pass resolved promises to the callback?

comment:3 Changed 6 years ago by Arlo White

In my hypothetical example, result1 is required, but the code can continue/recover if result2 is missing.

result2 would be null, undefined, or maybe the Error object? I'm not sure on the best design. Having access to the Error might be useful, but the code is a bit more complex that way.

if(result1 instanceof Error){
  // result1 is required for this code.
  throw result1;
}
// result2 is optional
if(!(result2 instanceof Error)){
  // result2 successfully retrieved.
}

comment:4 Changed 3 years ago by dylan

Milestone: tbd1.12
Resolution: patchwelcome
Status: newclosed

Given that no one has shown interest in creating a patch in the past 2+ years, I'm closing this as patchwelcome. Please let us know if you would like to get involved in helping make this change to Dojo!

Note: See TracTickets for help on using tickets.