autoparallel
v0.1.0
Published
Autoparallel is a JavaScript library that helps you parallelize promises automatically, resolving parameter dependencies as if it were magic
Readme
Autoparallel
Autoparallel is a JavaScript library that helps you parallelize promises automatically, resolving parameter dependencies as if it were magic 🪄.
Installation
Using npm:
npm install autoparallelUsing yarn:
yarn add autoparallelUsage
import { AutoParallel } from 'autoparallel'
interface Response {
id: number
parameter?: string
}
const method1 = (parameter: string) => {
return new Promise<Response>((resolve) => {
setTimeout(() => {
resolve({
id: 1,
parameter
})
}, 100)
})
}
const method2 = () => {
return new Promise<Response>((resolve) => {
setTimeout(() => {
resolve({ id: 2 })
}, 100)
})
}
const method3 = () => {
return new Promise<Response>((resolve) => {
setTimeout(() => {
resolve({ id: 3 })
}, 100)
})
}
(async () => {
console.time('executionTime')
const autoparallel = new AutoParallel()
.add('method1', method1, ['test'])
.add('method2', method2, (ctx) => [ctx.method1.id])
.add('method3', method3, (ctx) => [ctx.method2.id])
const result = await autoparallel.execute()
console.log(result)
console.timeEnd('executionTime')
})()This example returns:
{
method1: { id: 1, parameter: 'test' },
method2: { id: 2 },
method3: { id: 3 }
}
executionTime: 203.186msContributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
