promise-pool | Map-like , concurrent promise processing | Reactive Programming library
kandi X-RAY | promise-pool Summary
kandi X-RAY | promise-pool Summary
Map-like, concurrent promise processing for Node.js. Installation · Docs · Usage.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of promise-pool
promise-pool Key Features
promise-pool Examples and Code Snippets
Community Discussions
Trending Discussions on promise-pool
QUESTION
My question is basically a combination of
I'm aware of Promise.allSettled
, but I'm failing to find a good way to also limit concurrency.
What I have so far:
Idea 1 using p-limit
:
ANSWER
Answered 2020-Nov-20 at 16:29It's simple enough to implement it yourself - make an array of functions that, when called, return the Promise. Then implement a limiter function that takes functions from that array and calls them, and once finished, recursively calls the limiter again until the array is empty:
QUESTION
I use promises in NodeJS code all the time. My underlying, naive way of looking at the problem -
Free up the main thread by moving some work in the background by making it async (which ultimately is sync at some level)
While I used promise of promises and multiple promises, I don't understand them well. Is there a good explanation of how they actually work in NodeJS provided there are transactions that are synchronous at some level?
- For e.g. I have a DB operation that's async. While waiting for the DB operation to complete, the thread moves on to some other statements and starts executing them. Once the DB operation is complete, the execution engine picks it up again and starts executing the functions provided after promise resolve. Is this s fair statement to make?
- How then do libraries like
promise-pool
work? E.g. I start 10 different DB operations in async mode. Will they work similar to (1), just wait for resolve and carry on from there? If those subsequent operations after promise is synchronous, will promises get "queued up" to get executed? - By using the same "promise pool" what will happen if I start 10 different transactions that are synchronous and compete for CPU time (say finding out first 100K numbers in Fibonacci series)? Will they just queued up and are not really executed in parallel?
Thanks!
...ANSWER
Answered 2020-Sep-19 at 05:43For e.g. I have a DB operation that's async. While waiting for the DB operation to complete, the thread moves on to some other statements and starts executing them. Once the DB operation is complete, the execution engine picks it up again and starts executing the functions provided after promise resolve. Is this s fair statement to make?
Yes, more or less. Although it's unlikely the engine itself that does this. Somewhere deeper in your database library there's likely a TCP socket. The engine will queue up event executions when data arrives in these sockets, and eventually this trickles down to your database library resolving the promise.
How then do libraries like promise-pool work? E.g. I start 10 different DB operations in async mode. Will they work similar to (1), just wait for resolve and carry on from there? If those subsequent operations after promise is synchronous, will promises get "queued up" to get executed?
On a high level the way these libraries work is that they are given 'some function that returns a promise'. When given many, it keeps count how many of these functions have been called, and for which the promise has not resolved yet. If it's given more of these functions than the size of the pool, it keeps these functions in an array of sorts and call them when that number reduces.
By using the same "promise pool" what will happen if I start 10 different transactions that are synchronous and compete for CPU time (say finding out first 100K numbers in Fibonacci series)? Will they just queued up and are not really executed in parallel?
Javascript engines execute code synchronously. Something like a fibonacci sequence will execute all at once, which will cause the triggering of any io and timing events to be delayed.
As a closing note, I found it helpful to read the source of a Promise. They're only a few dozen lines of code and there's nothing inherently part of a promise that is async. Aside perhaps that when a callback passed to then()
is supposed to be triggered, it will always happen slightly later.
QUESTION
Recently had to detect recursions in a directed graph in code logic. My nodejs implementation feels complex and I am now wondering:
- Are there any problems with the code?
- Can we simplify it / make it more readable?
ANSWER
Answered 2020-Mar-29 at 16:06We may shorten it a bit:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install promise-pool
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page