Socket
Book a DemoInstallSign in
Socket

multiplex-promise

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multiplex-promise

Multiplex a Promise execution

1.0.4
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

MultiplexPromise

Multiplex a Promise execution

This module is intended to wait for the same promise result in different parallels executions.

As an example it could be used to wait for a token renew inside an axios interceptor.

Be aware that if your promise returns a non primitive value all the awaiters will get the same reference so clone it if you need to modify it.

Quick example:

async function meassureExecution(op: () => Promise<any>) {
    var hrstart = process.hrtime()
    var start = new Date()
    await op();
    var end = new Date().getTime() - start.getTime();
    const hrend = process.hrtime(hrstart);
    console.info('Execution time: %dms', end)
    console.log('Execution time (hr): %dms', (hrend[0] * 1000) + (hrend[1] / 1000000))
}
export async function testMultiplex() {
    let executionsCount = 0;
    const longTask = () => {
        return new Promise<number>(resolve => {
            setTimeout(() => {
                for (let a = 0; a < 100000000; a++) { }
                executionsCount++;
                resolve(1);
            }, 100);
        });
    }
    const longTaskMultiplexed = new MultiplexPromise(longTask);
    let promises: Promise<any>[] = [];
    let resultCounter = 0;
    for (let i = 0; i < 10; i++) {
        promises.push(longTask().then(() => resultCounter++));
    }
    console.log('No Multiplex')
    await meassureExecution(() => Promise.all(promises));
    console.log('Executions: ' + executionsCount);
    console.log('Results count: ' + resultCounter);
    resultCounter = 0;
    executionsCount = 0;
    promises = [];
    for (let i = 0; i < 10; i++) {
        promises.push(longTaskMultiplexed.run().then(() => resultCounter++));
    }
    console.log('Multiplex')
    await meassureExecution(() => Promise.all(promises));
    console.log('Executions: ' + executionsCount);
    console.log('Results count: ' + resultCounter);
}

Fn testMultiplex outputs:

No Multiplex
Execution time: 747ms
Execution time (hr): 746.136796ms
Executions: 10
Results count: 10
Multiplex
Execution time: 165ms
Execution time (hr): 165.597155ms
Executions: 1
Results count: 10

Keywords

promise

FAQs

Package last updated on 02 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.