https://github.com/innei/next-async
A simple implementation of koa middleware.
https://github.com/innei/next-async
Last synced: 5 months ago
JSON representation
A simple implementation of koa middleware.
- Host: GitHub
- URL: https://github.com/innei/next-async
- Owner: Innei
- Created: 2022-10-24T15:27:03.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-18T08:06:53.000Z (over 2 years ago)
- Last Synced: 2025-02-03T23:49:04.431Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 546 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: readme.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Next Async
A simple implementation of koa middleware.
```ts
import { Co } from '@innei/next-async'
const co = new Co()
co.use(someMiddlewares)
await co.start()
```
## Usage
**Using middleware**
```ts
const middleware = function (args) {
this.next()
}
co.use(middleware)
```
**Pass context to `Co`**
```ts
const co = new Co({ data: [] })
co.use(
function () {
this.data.push(1)
this.next()
},
function () {
console.log(this.data) // [1]
this.next()
console.log(this.data) // [1, 2]
},
function () {
this.data.push(2)
},
).start()
```
**Use async runner and await next runner done then back**
```ts
const co = new Co({ data: [] })
await co
.use(
function () {
this.data.push(1)
this.next()
},
async function () {
console.log(this.data) // [1]
await this.next()
console.log(this.data) // [1, 2]
},
async function () {
await 1
this.data.push(2)
},
)
.start()
```
**Abort action in flow**
```ts
const co = new Co({ data: [] })
co.use(
function () {
this.data.push(1)
this.next()
},
function () {
this.abort()
},
function () {
this.data.push(2) // will do not run
},
).start()
```
## Options
```ts
const co = new Co(ctx, options)
```
| Key | Type | Default | Description |
| --------------- | ------- | ------- | ------------------------------------------------- |
| automaticNext | boolean | false | Always run next action when this action returned. |
| catchAbortError | boolean | true | Catch CoAbortError or not. |
## License
2022 © Innei, Released under the MIT License.
> [Personal Website](https://innei.ren/) · GitHub [@Innei](https://github.com/innei/)