When working with the Twitter API, I need to make recursive method calls to retrieve tweets since each request only returns a maximum of 100 tweets.
The process is straightforward:
- Call the function and await it
- Make an HTTP request and await that
- If the response metadata contains a
next_token
, cache the results and make another HTTP request using the next token. - Repeat until the
next_token
is undefined, then resolve the promise with the list of all tweets.
However, something seems to be going wrong. The recursive HTTP calls work fine, but when the else block in the recursive function is executed and the promise is resolved, nothing happens. Execution doesn't return to the initial function. It's almost like everything is just spinning without progress. Even setting breakpoints on every line does not trigger any breakpoints.
What could be causing this issue?
public async getTweetList(ticker: string): Promise<string[]>{
let tweets: string[] = [];
tweets = await this.getAllTweetsRecursively(ticker, null, tweets);
return tweets;
}
public async getAllTweetsRecursively(ticker: string, nextToken: string, tweetList: string[]): Promise<string[]>{
return new Promise(async (resolve, reject) => {
let query = `?query=(${ticker})`
query += this.extraQuery;
if(nextToken){
query += this.nextTokenQuery + nextToken
}
let res = await axios.default.get(this.url + query, {
headers: this.headers
})
let newNextToken = res.data.meta.next_token;
if(res.data.data.length > 0 && newNextToken){
res.data.data.forEach(tweet => {
tweetList.push(tweet.text);
})
this.getAllTweetsRecursively(ticker, newNextToken, tweetList);
}
else {
resolve(cleanedTweets)
}
})
}
Alternatively, here's another implementation facing the same issue:
public async getTweetList(ticker: string): Promise<string[]>{
return new Promise(async (resolve) => {
let tweets: string[] = [];
tweets = await this.getAllTweetsRecursively(ticker, null, tweets);
resolve(tweets);
})
}
public async getAllTweetsRecursively(ticker: string, nextToken: string, tweetList: string[]): Promise<string[]>{
return new Promise(async (resolve, reject) => {
let query = `?query=(${ticker})`
query += this.extraQuery;
if(nextToken){
query += this.nextTokenQuery + nextToken
}
let res = await axios.default.get(this.url + query, {
headers: this.headers
})
let newNextToken = res.data.meta.next_token;
if(res.data.data.length > 0 && newNextToken){
res.data.data.forEach(tweet => {
tweetList.push(tweet.text);
})
await this.getAllTweetsRecursively(ticker, newNextToken, tweetList);
}
else {
let cleanedTweets: string[] = [];
tweetList.forEach(tweet => {
if(tweet.startsWith("RT")){
return;
}
if(!tweet.toLowerCase().includes("$" + ticker)){
return;
}
cleanedTweets.push(tweet);
});
resolve(cleanedTweets)
}
})
}