2016-10-09 41 views
1

我正在試驗NodeJS和twitter API。我需要一個承諾的幫助。功能requestFollowers應該返回一個承諾,它會。當我在節點cli中運行文件時,它表示處理並且從不記錄該值。我如何獲得我期望的價值,或者我如何解決它?無法通過Twitter API獲得承諾價值

這是我有這樣的。

function requestFollowers(tweep) { 
 
    return new Promise(function(resolve, reject) { 
 
    twitter.get('followers/list', { 
 
     count: 200, 
 
     skip_status: true, 
 
     screen_name: tweep 
 
    }, function(error, followers) { 
 
     if (error) { 
 
     console.log('followers list/ error >', error); 
 
     reject(error); 
 
     } else { 
 
     resolve(followers.users.map(thing => thing.screen_name)); 
 
     } 
 
    }); 
 
    }); 
 
} 
 

 
function onMention(error, tweets) { 
 
    if (error) { 
 
    console.log('mentions_timeline/ error >', error); 
 
    } else { 
 
    //console.log('mentions_timeline/ tweets >', tweets); 
 
    let mentioned = tweets[0].entities.user_mentions 
 
     .filter(thing => thing.screen_name !== user.screen_name) 
 
     .map(thing => thing.screen_name); 
 

 
    var list1 = requestFollowers(mentioned[0]), 
 
     list2 = requestFollowers(tweets[0].user.screen_name); 
 

 
    console.log('list1 >', list1.then(val => val).catch(error => error)); 
 
    console.log('list2 >', list2.then(val => val).catch(error => error)); 
 
    } 
 
} 
 

 
var config = require('./config'), 
 
    Twitter = require('twitter'), 
 
    twitter = new Twitter(config), 
 
    user = { 
 
    screen_name: 'screen_name' 
 
    }, 
 
    /** @param {string} status */ 
 
    getStatus = status => ({ 
 
    status 
 
    }); 
 

 

 
twitter.get('statuses/mentions_timeline', user, onMention);

回答

1

可以將此行console.log('list1 >', list1.then(val => val).catch(error => error));更改爲類似

list1.then(console.log).catch(console.error); 

你有什麼是傳遞一個未解決的承諾鏈log和日誌之前沒有解決的爭論的承諾打印它們 - 它的同步。另外,您的then(val => val)也是多餘的,即使這可以以某種方式工作 - 您不需要另一個只返回其輸入的函數。

+0

是的!謝謝你的正確答案。 – colecmc