Promise returns undefined

I know you cannot make an asynchronous function behave synchronously, but how do you add some ordering to the promises chain?

One result depends on the previous value of the promise, and when it doesn't, I get an undefined error. This is an HTTP request, so it relies on external factors like how fast my connection can complete the request, etc.

module.exports.movieCheck = function(authToken) {
return request({
    method : 'GET',
    uri : 'https://graph.facebook.com/' + profileID + '/posts?fields=message&limit=25&' + authToken
    }).spread(function (response, body) {
        console.log('https://graph.facebook.com/' + profileID + '/posts?fields=message&limit=25&' + authToken);
        return body;
    }, function(e) {
        console.log(e);
});
};

      

I am calling the above method as follows. However console.log returns undefined.

movieCheck.getToken()
.then(function(token) {
  movieCheck.movieCheck(token);
})
.then(function(movies) {
  console.log(movies); //should print json data
});   

      

Terminal fingerprints

undefined
https://graph.facebook.com/.../posts?fields=message&limit=25&access_token=....

      

+3


source to share


1 answer


Try to return the promise from the first and then the callback



movieCheck.getToken()
    .then(function (token) {
    return movieCheck.movieCheck(token);
}).then(function (movies) {
    console.log(movies); //should print json data
});

      

+5


source







All Articles