LOGIN
Robert Lancer
Chief Technology Officer at Collaborizm (Project Leader)
 · New YorkU.S.
Edit
Delete
Share
Report
INSPIRATION

Handling concurrency in Javascript and Node can be problematic.

One of the very confusing tasks can be how to properly execute a series of concurrent operations.

It’s not hard to execute them all at once, but what if you want more fine grained control of how many operations should occur simultaneously?

I created this utility function which takes an array of functions, each function returns a promise, it then also allows you to pass in a concurrency level.

The function returns a promise so it’s easy to perform an operation like closing a database connection when it’s finished.

function  ({promises, concurrency})  {

  console.log(`Digesting ${promises.length} promises with a concurrency or ${concurrency}`);

  return new Promise(function (resolve, reject) {

    let failed = 0, resolved = 0, index = 0;

    const next = () => {
      if (index < promises.length) {
        runAndIncrement(index);
        index++;
        return false;
      }
      else if (resolved + failed === promises.length) {
        resolve({failed, resolved});
        return true;
      }
    };

    const runAndIncrement = index => {
      promises[index]().then(resp=> {
        console.log(`Resolved ${index + 1} out of ${promises.length} >>`, resp || ' void');
        resolved++;
        next();
      }).catch(resp=> {
        console.log(`Rejected ${index + 1} out of ${promises.length} >>`, resp || ' void');
        failed++;
        next();
      });
    };

    for (let i = 0; i < concurrency; i++)
      if (next())
        break;
  });
};

We use this function in a few places in Collaborizm on our backend.

Enjoy!

2 likes 
Like
Share
Bi Sh
Design Invention Research
 · KathmanduNepal
Edit
Delete
Share
Report

How can we measure its Improvement impacts? can we feel it? #curious
I think if these kind of Finer Improvements be incorporated in JavaScript itself (by design) will make things more efficient. A feedback to Java may be… #thinking

0 like 
Robert Lancer
Chief Technology Officer at Collaborizm (Project Leader)
 · New YorkU.S.
Edit
Delete
Share
Report

Bi Sh, this can’t be felt because it handles background tasks. And it would be nice for this to be incorporated into the language, something like Promises.all is very similar but does not allow you to set a concurrency level.

0 like 
Leave a reply...
DISCOVER
CHAT
ALERTS
-2
DISCUSSIONS
-1
FEED