3.0.0 • Published 2 years ago

do-times v3.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
2 years ago

do-times

Call an async function n times and await the promise returned by it.

Install

npm install do-times

On engines that do not support async/await, use version 2.x.x:

npm install do-times@2

Usage

import doTimes from 'do-times'
// const doTimes = require('do-times')

doTimes(3, async (t, i) => new Promise(resolve => {
    setTimeout(() => {
        console.log({t, i})
        resolve(`time_${t}`)
    }, 1000)
}))
.then(values => console.log(values))
//=> {t: 1, i: 0}
//=> {t: 2, i: 1}
//=> {t: 3, i: 2}
//=> ['time_1', 'time_2', 'time_3']
 
// Sync equivalent
doTimes(3, (t, i) => {
    console.log({t, i})
    return `time_${t}`
})
.then(values => console.log(values))
 //=> {t: 1, i: 0}
 //=> {t: 2, i: 1}
 //=> {t: 3, i: 2}
 //=> ['time_1', 'time_2', 'time_3']

The package is ~2kB unpacked, has no dependencies, includes a JSDoc reference, and is compatible with browsers that has support for async/await, arrow functions and const/let, and with node >=7.6.0.

License

MIT

2.1.1

2 years ago

3.0.0

2 years ago

2.1.0

2 years ago

1.0.3

5 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago