-1

I am using twilio platform ... and I am testing my code... but I dont understand what happen when I try to get a channel from channelDescriptor... I have this code:

function processChannelPage(page)
            {
                var items = page.items;
                that.channels = that.channels || [];

                for (let c = 0, p = Promise.resolve(); c < items.length; c++) 
                {
                    p = p.then(function() {
                        let channelDescriptor = items[c];                        
                        console.log("SID ", channelDescriptor.sid);
                        
                         
                        getPreviousMessages(channelDescriptor, that)
                        .then(function() {
                            console.log("resuelto msg", channelDescriptor.sid);
                            return Promise.resolve();
                        }); 

                    });    
                }

                ..............
            }

            that.client.getUserChannelDescriptors().then(function (paginator) {
                processChannelPage(paginator);
            });

    function getPreviousMessages(channelDescriptor, chat) {
        return new Promise(resolve => { 
            console.log("p2.....step0.....", channelDescriptor.sid);     
            console.log("p2.....step1.....", channelDescriptor.sid);
            let promise = chat.getChannel(channelDescriptor.sid); 
            console.log(promise);       
            return promise.then(channel => {
                console.log("p2.....step2.....", channelDescriptor.sid); 
                return channel;
            });
        });
    }
 

    TwilioChat.prototype.getChannel = function (channel_sid) {
        console.log("I am in getChannel.....");
        for (var channel in this.channels) {
            if (this.channels[channel].sid == channel_sid) {
                return this.channels[channel].getChannel();
            }
        }
        return null;
    };

I understand that TwilioChat.prototype.getChannel returns a Promise, then I know I need to evaluate this promise using THEN like this

chat.getChannel(channelDescriptor.sid).then

But I see this results:

SID  CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:147 p2.....step0..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:148 p2.....step1..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:72 SID  CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:147 p2.....step0..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:148 p2.....step1..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:150 p2.....step2..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:150 p2.....step2..... CH369ca3cc46d74886b031a52fd2e7dc29

My question is... why this log twilio_helper.js:150 p2.....step2 I see outside my Chain of promises. I need to see this result:

SID  CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:147 p2.....step0..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:148 p2.....step1..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:150 p2.....step2..... CH09c567eeebab4dabb5fa8594a44d774e

twilio_helper.js:72 SID  CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:147 p2.....step0..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:148 p2.....step1..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:150 p2.....step2..... CH369ca3cc46d74886b031a52fd2e7dc29

First channelDescriptor.... all promises executed.... next ChannelDescriptor... all promises executed... In other words the loop advance in order by each channelDescriptor... Please I need only Promises... not Async/Await... because I need this also works in IExplorer. Can you help me with this promises? Thank you very much!

Ok!!!! Ok I change my code like these modifications....

TwilioChat.prototype.getChannel = function(channel_sid){
    console.log("I am in getChannel.....");
    for (var channel in this.channels) {
        if (this.channels[channel].sid == channel_sid) {
            return this.channels[channel].getChannel();
        }
    }
    reject("error");
};


function getPreviousMessages(channelDescriptor, chat) {
    return new Promise(resolve => { 
        console.log("p2.....step0.....", channelDescriptor.sid);     
        console.log("p2.....step1.....", channelDescriptor.sid);
        let promise = chat.getChannel(channelDescriptor.sid); 
        console.log(promise);       
        return promise.then(channel => {
            console.log("p2.....step2.....", channelDescriptor.sid); 
            resolve(channel); // I Resolve my New Promise
        });
    });
}

But my test always results this:

I see....this code log....

SID  CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:147 p2.....step0..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:148 p2.....step1..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:72 SID  CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:147 p2.....step0..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:148 p2.....step1..... CH369ca3cc46d74886b031a52fd2e7dc29
twilio_helper.js:388 I am in getChannel.....
twilio_helper.js:392 Promise {<pending>}
twilio_helper.js:150 p2.....step2..... CH09c567eeebab4dabb5fa8594a44d774e
twilio_helper.js:150 p2.....step2..... CH369ca3cc46d74886b031a52fd2e7dc29

p2.....Step2..... is outside my loop.... Really I dont understand.

user1228739
  • 153
  • 3
  • 19
  • I see several problems with your code: `p` scope is only in the `for` loop, so you cannot return it to chain other work. You're also using the [promise constructor antipattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it), but then don't resolve the `new Promise` (you never *call* `resolve`). A function that is supposed to return a promise (getChannel) should never return `null`. Too many things are wrong here. Have you worked with promises before? – trincot Nov 29 '18 at 19:39
  • I see this sample... and I am trying to do something similiar...https://stackoverflow.com/questions/40328932/javascript-es6-promise-for-loop – user1228739 Nov 29 '18 at 19:50
  • Did you read all of my comment? Don't use the antipattern. Fix that first. – trincot Nov 29 '18 at 19:53
  • Yeah! I need to stop every item in loop, in other words I need to make sequential loop... but inside each iteration need a chain of promises... getChannel... getMessages...and others....Ok if getChannel could not return a null, I understand... I can "enter" a validation if (promise) {} then this it means that it's a valid promise... I am trying to understand promises programming with twilio sample... I need do a For Loop (each iteration must have a chain of promises... and this loop must advance in order... one by one). – user1228739 Nov 29 '18 at 19:53
  • Ok... But TwilioChat.prototype.getChannel returns **this.channels[channel].getChannel();** and this is a Promise... Because I see this result **Promise {}** If I use **.then(** this result goes outside my sequence...do you understand...what's going on? – user1228739 Nov 29 '18 at 20:00
  • Yes, I already wrote: you never call `resolve`, so your `new Promise` never resolves. But you should not go and now go call `resolve`, but you should fix it by not using the antipattern. Do you understand what I am saying? – trincot Nov 29 '18 at 20:03
  • I answer with code.... not as comment – user1228739 Nov 29 '18 at 20:38

1 Answers1

0

Avoid the Promise constructor antipattern in getPreviousMessages, make sure that getChannel always returns a promise, and don't forget to return your promises from the then callbacks in processChannelPage.

function processChannelPage(page) {
    var items = page.items;
    that.channels = that.channels || [];

    for (let c = 0, p = Promise.resolve(); c < items.length; c++) {
        p = p.then(function() {
            let channelDescriptor = items[c];                        
            console.log("SID ", channelDescriptor.sid);

            return getPreviousMessages(channelDescriptor, that).then(function() {
//          ^^^^^^
                console.log("resuelto msg", channelDescriptor.sid);
            }); 
        });
    }
    …
    return p;
}

that.client.getUserChannelDescriptors().then(function (paginator) {
    return processChannelPage(paginator);
//  ^^^^^^
});

function getPreviousMessages(channelDescriptor, chat) {
    console.log("p2.....step0.....", channelDescriptor.sid);     
    console.log("p2.....step1.....", channelDescriptor.sid);
    let promise = chat.getChannel(channelDescriptor.sid); 
    console.log(promise);       
    return promise.then(channel => {
//  ^^^^^^
        console.log("p2.....step2.....", channelDescriptor.sid); 
        return channel;
    });
}

TwilioChat.prototype.getChannel = function (channel_sid) {
    console.log("I am in getChannel.....");
    for (var channel in this.channels) {
        if (this.channels[channel].sid == channel_sid) {
            return this.channels[channel].getChannel();
        }
    }
    return Promise.resolve(null);
//         ^^^^^^^^^^^^^^^^    ^
};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375