Skip to content

compulim/promise-critical-section

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

promise-critical-section

Allows only one asynchronous operation to run at a time.

Although JavaScript is single-threaded, there are times you may want to limit number of asynchronous operation to enter a block simultaneously. For example,

  • Pooling a single resource
  • Running a series of asynchronous steps without interruptions

How to use

In the code below, Step 1A and Step 1B will be run serially regardless of race condition from Step 2.

import CriticalSection from 'promise-critical-section';

const section = new CriticalSection();

Promise.race([
  section.enter()
    .then(() => {
      // do something that is limited to one asynchoronous operation
      console.log('Step 1A');
    })
    .then(() => {
      // do another thing
      console.log('Step 1B');
    })
    .then(() => section.leave()),
  section.enter()
    .then(() => {
      // do something in the critical section
      console.log('Step 2');
    })
    .then(() => section.leave())
])

You can also write it with ES7 async/await. The following example maybe a bit exaggerated.

return Promise.all([
  (async () => {
    await section.enter();
    console.log('Step 1A');
    console.log('Step 1B');
    await section.leave();
  })(),
  (async () => {
    await section.enter();
    console.log('Step 2');
    await section.leave();
  })()
]);

Contributions

Like us? Please star us or give us suggestions.

Please file an issue to us with minimal repro.