Skip to content

A simple implementation of koa middleware.

Notifications You must be signed in to change notification settings

Innei/next-async

Repository files navigation

Next Async

A simple implementation of koa middleware.

import { Co } from '@innei/next-async'

const co = new Co()
co.use(someMiddlewares)
await co.start()

Usage

Using middleware

const middleware = function (args) {
  this.next()
}

co.use(middleware)

Pass context to Co

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

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

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

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 · GitHub @Innei