admin管理员组

文章数量:1392003

I know you can't make an asynchronous function behave synchronously but how do I add some kind of order to my promises chain?

One result relies on the previous promise value and when that doesn't happen I get an undefined error. It's an http request so it is relying on external factors like how fast my connection can execute the request, etc.

module.exports.movieCheck = function(authToken) {
return request({
    method : 'GET',
    uri : '/' + profileID + '/posts?fields=message&limit=25&' + authToken
    }).spread(function (response, body) {
        console.log('/' + 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 prints

undefined
/.../posts?fields=message&limit=25&access_token=....

I know you can't make an asynchronous function behave synchronously but how do I add some kind of order to my promises chain?

One result relies on the previous promise value and when that doesn't happen I get an undefined error. It's an http request so it is relying on external factors like how fast my connection can execute the request, etc.

module.exports.movieCheck = function(authToken) {
return request({
    method : 'GET',
    uri : 'https://graph.facebook./' + profileID + '/posts?fields=message&limit=25&' + authToken
    }).spread(function (response, body) {
        console.log('https://graph.facebook./' + 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 prints

undefined
https://graph.facebook./.../posts?fields=message&limit=25&access_token=....
Share Improve this question asked Jul 9, 2015 at 11:00 mremanemremane 1032 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Try to return the promise from the first then callback

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

本文标签: javascriptPromise returns undefinedStack Overflow