javascript异步和twitter的问题

Issues with javascript async and twitter

本文关键字:问题 twitter 异步 javascript      更新时间:2023-09-26

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

编辑:

我的理解是getTweets().done(getTopwords());调用getTweets(),当它完成时调用getTopwords()

代码:

// 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
  }
}

试试这个;)

// 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();