Javascript - Promises and forEach

Viewed 3052

I am trying to fill the array with avatarIcon node from parsed xmls but my final array in res.view is empty plus the execution doesn't seem to reach return res.view function. How do I do this correctly?

function parseXML(xml) {
 var parsed
 parseString(xml, { explicitArray: false }, function(err, result) {
     parsed = result
 })
 return parsed
}

function findUsers() {
 return new Promise(function(resolve, reject) {
    User.find().exec(function(err, sids) {
        resolve(sids)
    })
 })
}

avatars: function(req, res) {
    var arr = []
    findUsers().then(function(result) {
        result.forEach(function(el) {
            getProfileXML(el.sid).then(function(result) {
                 arr.push(parseXML(result).profile.avatarIcon)
            })

        })
        return res.view('users', {
            users: arr
        })

    })

}
1 Answers
Related