2016-10-09 7 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

あなたが前に引数の約束を解決しませんlogとログに未解決の約束チェーンを渡していた何

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

のようなものに、このラインconsole.log('list1 >', list1.then(val => val).catch(error => error));を変更することができますそれらを印刷する - その同期。また、あなたのthen(val => val)は何とか働くとしても冗長です。入力を返すだけの別の関数は必要ありません。

+0

はい!その正解をありがとうございます。 – colecmc

関連する問題