Home > Software design >  Porting Promise.all functionality from AngularJs to VueJs
Porting Promise.all functionality from AngularJs to VueJs

Time:11-09

I have this function that I need to move from AngularJs to VueJs and it has me stumped! Any help would be appreciated :)

items = {
    one: {...details here...},
    two: {},
}

AngularJs

var promises = [];
var deferred = $q.defer();

angular.forEach(items, function(details) {
    promises.push(details.promise);
});

$q.all(promises).finally(function() {
    deferred.resolve();
});

return deferred.promise;

What I have in VueJs so far

let promises = [];

for (let [name, details] of Object.entries(items)) {
    promises.push(new Promise((resolve) => {resolve(details)}));
}

return Promise.all(promises);

Thank you!

CodePudding user response:

There is no need for new Promise because Promise.all allows for non-promise values.

For a promise that resolves with undefined (this is what a deferred does) when all object values are settled (i.e. resolved or rejected), it's:

Promise.all(Objects.values(items).map(({ promise }) => promise))
.then(() => {}) // if it's necessary to resolve with undefined
.catch(() => {}) // if it's necessary to not reject

If all values aren't promises, there is no need for Promise.all at all.

  • Related