无法通过Twitter API获得承诺价值

时间:2016-10-09 02:35:54

标签: javascript node.js twitter

我正在尝试使用NodeJS和twitter API。我需要一个承诺的帮助。函数requestFollowers应该返回一个promise,它确实如此。当我在节点cli中运行该文件时,它表示处理并且从不记录该值。我如何获得我期望的价值或如何解决它?

这就是我所拥有的。

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 个答案:

答案 0 :(得分:1)

您可以将此行console.log('list1 >', list1.then(val => val).catch(error => error));更改为

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

你所拥有的是将未解决的保证链传递给log并且记录在打印之前不会解析参数的承诺 - 它是同步的。此外,then(val => val)也是多余的,即使这种方式可行 - 您不需要另一个只返回其输入的函数。

相关问题