1.0.0 • Published 4 months ago

promise-singleflight v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

promise-singleflight

NPM

Installation

$ npm install --save promise-singleflight

API

create<Key, ReturnValue>(): (key: Key, fn: () => Promise\<ReturnValue>) => Promise\<ReturnValue>;

Return a singleflight wrap function.

const PromiseSingleflight = require("promise-singleflight")
const singleflight = PromiseSingleflight.create()
singleflight(key, async function(){
    ...
    return value
})

or

import * as PromiseSingleflight from 'promise-singleflight'
const singleflight = PromiseSingleflight.create()
singleflight(key, async function(){
    ...
    return value
})

Usage Example

const PromiseSingleflight = require("promise-singleflight")
const singleflight = PromiseSingleflight.create()
function delay(ms) {
    return new Promise(function (resolve) {
        setTimeout(resolve, ms)
    })
}

async function queryData(path) {
    return singleflight(path, async function () {
        console.log(`start queryData ${path}`)
        await delay(1000)
        console.log(`end queryData ${path}`)
        return `data:${path}`
    })
}

(async function () {
    const items = await Promise.all([
        queryData('/a'),
        queryData('/a'),
        queryData('/b'),
    ])
    console.log(items.join('\n'))
})()

// Output:
// start queryData /a
// start queryData /b
// end queryData /a
// end queryData /b
// data:/a
// data:/a
// data:/b

Reference Docs

1.0.0

4 months ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.25

2 years ago

0.0.26

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.1

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.15

2 years ago

0.0.9

2 years ago

0.0.16

2 years ago

0.0.8

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.0

3 years ago