-
From rlogank Hello, I’ve spent the last week or so implementing Twitter Oauth 1.0a on my application, and I’ve finally got it set up, but I have run into a brick wall that I can’t seem to get past. Hoping you can shine some light on the situation. All I need to do is limit the amount of tweets that get pulled with this code. For example, I just want to get the last 10 tweets mentioning JavaScript. But it seems to just run until I hit the rate limit. Is there something really simple I’m missing? And if so, could you please explain how I can implement it? const jsTweets = await client.v2.search("JavaScript", {
"media.fields": "url",
});
for await (const tweet of jsTweets) {
console.log(tweet);
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You need to understand how the tweet paginator works. // This object is a paginator that holds, at first, the initial 1st page of result
jsTweets
// This is how you access in a Array structure the currently loaded tweets
jsTweets.tweets
// This is how you access the "includes" property linked to all loaded tweets yet
jsTweets.includes
// Those two loops are exactly the same and iterates over the LOADED tweets
for (const tweet of jsTweets) {
}
for (const tweet of jsTweets.tweets) {
}
// This is how you load the "next page"
await jsTweets.fetchNext()
// Now, it will iterate over first and second page
for (const tweet of jsTweets) {
}
// Hint: if you want to have first and second page in two separated objects,
// you can also use .next()
const secondPage = jsTweets.next()
// secondPage.tweets will not be present in jsTweets.tweets
// This loop is also known as "async iterator", and it is a custom iterator
// that automatically fetch tweets until your rate limit is hit
// NOTE the await!
for await (const tweet of jsTweets) {
} |
Beta Was this translation helpful? Give feedback.
You need to understand how the tweet paginator works.