简体   繁体   English

承诺返回undefined

[英]Promise returns undefined

I know you can't make an asynchronous function behave synchronously but how do I add some kind of order to my promises chain? 我知道你不能让异步函数同步运行但是如何在我的promises链中添加某种顺序?

One result relies on the previous promise value and when that doesn't happen I get an undefined error. 一个结果依赖于先前的promise值,当没有发生时,我得到一个未定义的错误。 It's an http request so it is relying on external factors like how fast my connection can execute the request, etc. 这是一个http请求,因此它依赖于外部因素,例如我的连接可以执行请求的速度等等。

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. 但是console.log返回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.com/.../posts?fields=message&limit=25&access_token=....

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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM