【问题标题】:Promise returns undefined承诺返回未定义
【发布时间】:2015-09-27 17:00:03
【问题描述】:

我知道你不能让异步函数同步运行,但是 如何在我的承诺链中添加某种订单?

一个结果依赖于先前的承诺值,当这种情况没有发生时,我得到一个未定义的错误。这是一个 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);
});
};

我调用上面的方法如下。但是 console.log 返回 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=....

【问题讨论】:

    标签: javascript node.js promise bluebird


    【解决方案1】:

    尝试从第一个 then 回调中返回承诺

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

    【讨论】:

    • 在使用 Promise 链时避免 .catch 块来捕获异常会花费你很多
    猜你喜欢
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多