Return of promise inside forEach

Viewed 2537
function getNamesById(nameIds) {
    var defer = $q.defer();

    var result = [];
    nameIds.forEach(function (nameId) {
        account.getNameById(nameId).then(function (name) {
            result[nameId] = name;
        });
    });
    defer.resolve(result);
    return defer.promise;
}

I have the above code witch is obviously not working as expected. I have to iterate an array of id's and construct another array having the id's as keys, and the values taken from an async function

1 Answers
Related