[英]Promise returns undefined
我知道你不能讓異步函數同步運行但是如何在我的promises鏈中添加某種順序?
一個結果依賴於先前的promise值,當沒有發生時,我得到一個未定義的錯誤。 這是一個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=....
嘗試從第一個回調中返回承諾
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.