Thursday 20 April 2017

waiting for promise inside a for loop (not work as expected)

I had to put this function as a promise , because it needs sometime to be done

function getUProf(tid)
{
    return new Promise(function(resolve,reject){
         bot.getUserProfile(tid, function (err, profile) {
             resolve(profile);
         });
    });
}

and here in these codes , the problem is ..

for (var prop in snapshot.val()) {
            if (snapshot.val()[prop].grouped == gname && prop!=tid)
            {
                 getUProf(tid).then(function (profile){
                     console.log(profile);
                     bot.sendTextMessage(prop, "[" + profile.first_name + "] : " + msg);
                 });
            }
         }

it should print the user data in this line //console.log(profile);

but it print undefined , how to achieve that , and even the promise solution did not work



via Ahmed Mohsen

No comments:

Post a Comment