Last active
March 29, 2021 09:12
-
-
Save Aaronius/46ae4a0f8ff052cd24f0 to your computer and use it in GitHub Desktop.
Angular $q allSettled() implementation similar to https://github.com/kriskowal/q/wiki/API-Reference#promiseallsettled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('qAllSettled', []).config(function($provide) { | |
$provide.decorator('$q', function($delegate) { | |
var $q = $delegate; | |
$q.allSettled = function(promises) { | |
return $q.all(promises.map(function(promise) { | |
return promise.then(function(value) { | |
return { state: 'fulfilled', value: value }; | |
}, function(reason) { | |
return { state: 'rejected', reason: reason }; | |
}); | |
})); | |
}; | |
return $q; | |
}); | |
}); |
@ryanhart2 Your version is better but it should also consider "non-thenables" as successfully resolved values.
Implemented this and added it to the bower registry:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$q.all accepts an array or hash of promises, whereas this implementation of allSettled will only work when promises is an array of promises. An alternative could be as follows: