javascript async和twitter的问题

时间:2016-03-29 11:46:25

标签: javascript twitter

我目前有代码和功能(如下所示)用于获取推文,然后查看推文数据以返回集合中最常用的单词。我的问题是如何以异步方式运行代码?到目前为止,我已经尝试了getTweets().done(getTopwords());,但这只是返回undefined,我不确定这是否是我的代码的问题,或者这没有达到预期的效果。每个单独的功能都可以独立工作。

编辑:

我的理解是getTweets().done(getTopwords());调用了getTweets(),并且完成了调用getTopwords()

CODE:

// placeholder variables
var profile = 'MCFC',
    keyword = '',
    count = 10,
    date = '2015-11-11',
    lan = 'en',
    search = keyword + " since:" + date + " lang:" + lan,
    tweettxt = [],
    users = [];

// function for searching through twitter using the specified data
function getTweets(){
  return client.get('search/tweets', { q: search, count: count, from: profile },
              handleTweets)
}

// function for handling the tweets
function handleTweets(err, data){
  if (err) {
    console.error('Get error', err)
  }  
  else {
    return sortTweets(data);
  }
}

// function for getting the frequency of each word within a string
function getFreqword(){
  var string = tweettxt.toString(), // turn the array into a string
      changedString = string.replace(/,/g, " "), // remove the array elements 
      split = changedString.split(" "), // split the string 
      words = []; // array for the words

  for (var i=0; i<split.length; i++){
    if(words[split[i]]===undefined){
      words[split[i]]=1;
    } else {
      words[split[i]]++;
    }
  }
  return words;
}

// function for returning the top 20 words from getFreqword()
function getTopwords(){
  var topwords = getFreqword(),
      toptwenty = [];

  for (var i=0; i<=20; i++){
    toptwenty.push(topwords[i])
    toptwenty.sort(function(a, b){return a-b});
  }
  return toptwenty
}

编辑2:

// function for sorting through the tweets to return relevant information
function sortTweets (data) {
  for (var indx in data.statuses){
    var tweet = data.statuses[indx];
    tweettxt.push(tweet.text); // push the tweet text so it can be sorted for the most frequent words
    users.push(tweet.user.screen_name); // push the twitter user screen name so it can be sorted to find the most frequent users
  }
}

1 个答案:

答案 0 :(得分:1)

试试这个;)

// placeholder variables
var profile = 'MCFC',
  keyword = '',
  count = 10,
  date = '2015-11-11',
  lan = 'en',
  search = keyword + " since:" + date + " lang:" + lan,
  tweettxt = [],
  users = [];

// function for searching through twitter using the specified data
function getTweets(){
  client.get('search/tweets', {
    q: search,
    count: count,
    from: profile
  },
  handleTweets);
}

// function for handling the tweets
function handleTweets(err, data){
  if(err){
    console.error('Get error', err)
  }
  else{
    sortTweets(data);

    /* call getTopwords() as we sorted tweets */
    toptwenty = getTopwords();

    /* now you can access toptwenty */
    console.log(toptwenty);
  }
}

// function for getting the frequency of each word within a string
function getFreqword(){
  var string = tweettxt.toString(), // turn the array into a string
    changedString = string.replace(/,/g, " "), // remove the array elements 
    split = changedString.split(" "), // split the string 
    words = []; // array for the words

  for(var i = 0; i < split.length; i++){
    if(words[split[i]] === undefined){
      words[split[i]] = 1;
    }else{
      words[split[i]]++;
    }
  }
  return words;
}

// function for returning the top 20 words from getFreqword()
function getTopwords(){
  var topwords = getFreqword(),
    toptwenty = [];

  for(var i = 0; i < 20; i++){
    toptwenty.push(topwords[i])
    toptwenty.sort(function(a, b){
      return a - b
    });
  }
  return toptwenty
}

// function for sorting through the tweets to return relevant information
function sortTweets(data){
  for(var indx in data.statuses){
    var tweet = data.statuses[indx];
    tweettxt.push(tweet.text); // push the tweet text so it can be sorted for the most frequent words
    users.push(tweet.user.screen_name); // push the twitter user screen name so it can be sorted to find the most frequent users
  }
}

getTweets();