1

I am setting up a Sync Application using Twilio's Sync Library. For some reason, none of the REST API methods seem to work. That is, I cannot get any of the sync methods to console.log() anything via the runtime functions.

I can, however, console.log() plain text.

Here is my code:

exports.handler = function(context, event, callback) {

    // 0. Init
    // const phoneNumber   = event.phoneNumber;
    const issueLimit    = 3; // CHANGE IF NEEDED
    const listName      = 'issues';
    const twilioClient  = context.getTwilioClient();


    // 1. List all  lists 
    twilioClient.sync.services(context.SYNC_SERVICE_SID)
       .syncLists
       .list({limit: 20})
       .then(syncLists => syncLists.forEach(s => console.log(s.sid)));


    // 2. return true if quota reached

    console.log("Got to here");


    // 3. return false
    callback(null, undefined);
};

The only code that appears to execute is the 'console.log("Got to here");'. I'm also not receiving any error messages.

Any guidance is sincerely appreciated.

1 Answers1

1

When you see .then(), that's a promise, and you can read more about this here https://www.twilio.com/blog/2016/10/guide-to-javascript-promises.html

In other words, the JavaScript engine goes to your steps 2. and then 3. without waiting for 1. to finish. And since you're returning at step 3 with callback(null, undefined); you won't see the logs.

So, you'll have to move callback() inside the .then(), something like this:

exports.handler = function (context, event, callback) {

    // 0. Init
    // const phoneNumber   = event.phoneNumber;
    const issueLimit = 3; // CHANGE IF NEEDED
    const listName = 'issues';
    const twilioClient = context.getTwilioClient();


    // 1. List all  lists 
    twilioClient.sync.services(context.SYNC_SERVICE_SID)
        .syncLists
        .list({ limit: 20 })
        .then(
            function (syncLists) {
                console.log("Got to here");
                syncLists.forEach(s => console.log(s.sid));
                callback(null, undefined);
            }
        );


};
Alex Baban
  • 11,312
  • 4
  • 30
  • 44