You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The library already does a pretty good job of making rate limits more explicit, but I was wondering if it could go one step further and actually delay your calls (for example with a new fetchLastWhileWaiting() function) to make them respect the rate limit? Or would that be beyond the scope of the library?
Thanks. What kind of strategy would you use if you had to potentially run a script for multiple days because of the rate limits then? A cronjob wouldn't be able to adjust to always respect the shortest waiting time possible so it wouldn't be very efficient, but I guess it would be the simplest solution…
You can await when there is a rate limit error, as suggested:
functionsleep(ms: number){returnnewPromise(resolve=>setTimeout(ms,resolve));}asyncfunctionautoRetryOnRateLimitError<T>(callback: ()=>T|Promise<T>){while(true){try{returnawaitcallback();}catch(error){if(errorinstanceofApiResponseError&&error.rateLimitError&&error.rateLimit){constresetTimeout=error.rateLimit.reset*1000;// convert to ms time instead of seconds timeconsttimeToWait=resetTimeout-Date.now();awaitsleep(timeToWait);continue;}throwerror;}}}// Get your paginator hereconstpaginator=awaitclient.v2.following('12',{asPaginator: true,max_results: 1000});letcurrentPage=paginator;while(true){// Handle results of current page inside {currentPage.users}// Do stuff here...if(currentPage.users.length===0){break;// There's nothing, paginator ends :)}// Fetch the next page, and await if's there is a rate limit errorcurrentPage=awaitautoRetryOnRateLimitError(()=>paginator.next());}
Activity
alkihis commentedon Sep 21, 2021
Hi,
You can still implement by yourself a loop of
fetchLastusing this model.I don't personally think it's a good idea to allow a
Promiseto lock during multiple minutes... but it can be discussed :)SachaG commentedon Sep 21, 2021
Thanks. What kind of strategy would you use if you had to potentially run a script for multiple days because of the rate limits then? A cronjob wouldn't be able to adjust to always respect the shortest waiting time possible so it wouldn't be very efficient, but I guess it would be the simplest solution…
alkihis commentedon Sep 21, 2021
You can await when there is a rate limit error, as suggested:
SachaG commentedon Sep 22, 2021
Thanks for your help, I'll give this a try :)