Although Javascript is designed to be single threaded, you could still do things concurrently.
For example, we can read multiple files concurrentl...
For further actions, you may consider blocking this person and/or reporting abuse
I implemented this whole thing with a working code, let me know if I am doing something incorrectly.
Note: I am using setTimeout for async calls so promise failure will not happen.
hey, tried your code, did not work...
so, assigned the same delay for all of those...like 2 seconds... I did expect to see it process 2, schedule + 2; so, the result would be observable every 2 seconds and everytime 2 task
using the corrections from @kusha and the data and functino of your code it did work as expected!
hurray!
exceptional implementation to deal with REST API
Can you post your working snippet here?
hey I'm terrible with markdown, but the example bellow is in a lib, basically it does limit the number of active connections to a http API rest service to the a defined number (8 in this case), so a the http request is resolved, it start another connection keeping always 8 active connections to the API server; it will completely hide the connection/handshake delay while guarantee no 429 error (too many requests); from my experience fastest safe approach as you can know the maximum number of calls per second of the API service
Great article! Some minor improvements.
This is nice! 👍👍👍
Thank you so much for this post, learn a lot from this brilliant idea, also I added types (typescript) for this function in case someone needs this:
As written,
asyncLimit
will not resolve until the async function completes, because ofawait p.catch(() => {});
.Instead,
But we need the async function
fn
to complete before resolving theasync function (...args) {
. Isn't it?It's difficult to mentally follow the chain of promises here, because you have an async function (
const asyncLimit = async...
) which returns an async function (return async function
) which itself awaits at least two promises (one in a loop) before resolving.Note that
asyncLimit
does not have to be async, as it does not useawait
; removingasync
from the function signature would help comprehension a bit, since you would reduce one level of Promise-ception and stay out of limbo.I guess it doesn't really matter, because the promise returned by
asyncLimit
can be used by the calling code. But attaching afinally
clause top
instead ofawait
ing it allows the function to return immediately, instead of waiting forp
to resolve.oh, I mistyped haha! Thanks for catching this!
asyncLimit
shouldn't be an async function.No, because the function returns
p
itself, not the chained promise. The caller can attach its own.catch()
clauses top
.As in,
Thanks!! Benjamin's reply is accurate! :)