承诺返回undefined [英] Promise returns undefined

查看:207
本文介绍了承诺返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你不能让异步函数同步行为但
如何在我的promises链中添加某种顺序?

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

一个结果依赖于先前的promise值,当没有发生时,我得到一个未定义的错误。这是一个http请求,因此它依赖于外部因素,例如我的连接可以执行请求的速度等等。

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

我按以下方式调用上述方法。但是console.log返回undefined。

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

终端打印

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

这篇关于承诺返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆