https://github.com/linkdd/arecursion-js
Asynchronous recursion without maximum depth
https://github.com/linkdd/arecursion-js
async javascript recursion
Last synced: 9 months ago
JSON representation
Asynchronous recursion without maximum depth
- Host: GitHub
- URL: https://github.com/linkdd/arecursion-js
- Owner: linkdd
- License: mit
- Created: 2020-12-20T20:02:39.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-03-05T18:04:43.000Z (about 3 years ago)
- Last Synced: 2025-03-14T18:46:21.808Z (about 1 year ago)
- Topics: async, javascript, recursion
- Language: JavaScript
- Homepage:
- Size: 253 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
Asynchronous Recursion without Maximum Depth
## :alembic: How does it work ?
Thanks to the `setImmediate` function, the next iteration of the recursion is
scheduled on the next frame of the event-loop.
The current iteration will return, removing the function from the call stack.
The whole recursion is wrapped in a promise that will resolve to the result of
your recursive function
## :package: Installation
```
$ yarn add arecursion-js
```
## :wrench: Usage
```javascript
const recursion = require('arecursion-js')
const factorial = recursion.doAsync((n, acc) => {
if (typeof acc === 'undefined') {
acc = 1
}
if (n > 1) {
return recursion.iterate(n - 1, acc * n)
}
else {
return recursion.end(acc)
}
})
const main = async () => {
console.log(await factorial(5))
}
main()
```
Or with an asynchronous function:
```javascript
const recursion = require('arecursion-js')
const slow_sum = recursion.doAsync(async (n, acc) => {
if (typeof acc === 'undefined') {
acc = 0
}
await some_slow_task()
if (n > 1) {
return recursion.iterate(n - 1, acc + n)
}
else {
return recursion.end(acc)
}
})
const main = async () => {
console.log(await slow_sum(5))
}
main()
```
## :memo: License
This package is released under the terms of the [MIT License](LICENSE.txt).