Giter Club home page Giter Club logo

asynchronous-concurrency-primitives-js's Introduction

asynchronous concurrency primitives js

Node.js CI

Open in Codeflow

There are a few asynchronous concurrency primitives js:

Also see tests

Get started:

deps:

npm i

run tests:

npm test

Barrier

const Coroutines = [0, 1, 2, 3].map((n) => `coroutine ${n}`)

// Global variables
const b = new Barrier()
const c = new ColorLog()

;(async () => {
  c.log(`Lock barrier`, Coroutines[0])
  await b.Lock()
  c.log(`Done`, Coroutines[0])
})()

;(async () => {
  c.log(`Sleep 1s ...`, Coroutines[1])
  await sleep(1)
  b.UnLock()
  c.log(`barrier UnLock was invoked :)`, Coroutines[1])
})()

image

Mutex

// setup
const CoroutineS_NUMBER = 3
const Coroutines = [...Array(CoroutineS_NUMBER).keys()].map(
  (n) => `coroutine ${n}`
)
const sleepTimes = [...Array(CoroutineS_NUMBER).keys()]

// Global variables
const m = new Mutex()
const c = new ColorLog()

const CreateWorker = (sleepTime, workerNumber) => async () => {
  c.log(`Lock mutex`, Coroutines[workerNumber])
  await m.Lock()
  c.log(`In CS`, Coroutines[workerNumber])
  c.log(`Sleep ${sleepTime}s ...`, Coroutines[workerNumber])
  await sleep(sleepTime)
  c.log(`Out CS`, Coroutines[workerNumber])
  m.UnLock()
  c.log(`mutex UnLock was invoked :)`, Coroutines[workerNumber])
  c.log(`Done`, Coroutines[workerNumber])
}

;(async () => {
  const promises = sleepTimes
    // create workers
    .map((sleepTime, workerNumber) => CreateWorker(sleepTime, workerNumber))
    // execute workers
    .map((worker) => worker())
  // wait all tasks
  await Promise.all(promises)
})()

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.