• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Promises and async await in JavaScript

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted to design and implement a rate limiter, which allows the following usage:
From my understanding, when somebody calls do
, the rate limiter needs to do one of two things with the passed-in lambda:

Start executing it immediately
Queue it up for later execution

In my case, if i call do
three times in rapid succession, the first two async functions should be immediately started while the third should be queued up.

Because the rate limiter needs to monitor the state of any async functions that it starts, it needs to install a callback on the returned Promise. It's easiest to use Promise.finally to do that. The finally
callback will run whether the promise is completed or rejected, so it'll handle both success and failure cases.

When any in-flight async function finishes and my installed Promise.finally
handler is called, i will need to determine if there are any queued async functions. If so, i will need to start one.

So there are essentially two "events" that the rate limiter is concerned with:

-Somebody calls do
-One of the spawned tasks completes (either successfully or unsuccessfully)



can anyone try to implement it?
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic