3

We are developing alexa skill using alexa-app, in one of our intent we are trying to fetch albums from facebook and on success/failure we want alexa to respond accordingly. But intent is not waiting for FB call to complete. Below is the code snippet we are using:

function fetchAlbums(){
  return new Promise(resolve =>{
   FB.api("/me/albums", function (res) {
    if (res && !res.error) {
        // If we have data
        if (res.data) {
            resolve("Got Albums");
        } else {
            // REPORT PROBLEM WITH PARSING DATA
            resolve("Error getting albums");
        }
    } else {
        // Handle errors here.
        resolve("Error hitting endpoint");
    }
  });
 });
}

alexaApp.intent("readFeedIntent", {
  "utterances": [
    "read my facebook feed", "read facebook feed", "read feed"
  ]
},
function(request, res1) {
  // Again check if we have an access token
  if(request.hasSession() && !accessToken){
   accessToken = request.data.session.user.accessToken;
   FB.setAccessToken(accessToken);
  }
  if (accessToken) {
    var session = request.getSession();
    fetchAlbums().then(function(data){
        console.log(data);
        res1.say(data);
    });
  } else {
    res1.say(noAccessToken, tryLaterText).send();
  }
});

It is not throwing any errors, but Alexa is not speaking the anything where I can see the response in the console logs.

If I add: res1.say("Whatever!") at the end of function, then Alexa will speak 'Whatever' in response to this intent.

1 Answers1

0

Got it solved my myself:

instead of this:

fetchAlbums().then(function(data){
      console.log(data);
      res1.say(data);
})

you have to return it, like:

return fetchAlbums().then(function(data){
      console.log(data);
      res1.say(data);
})