0.3.0 • Published 2 years ago

@innei/next-async v0.3.0

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

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)
KeyTypeDefaultDescription
automaticNextbooleanfalseAlways run next action when this action returned.
catchAbortErrorbooleantrueCatch CoAbortError or not.

License

2022 © Innei, Released under the MIT License.

Personal Website · GitHub @Innei

0.3.0

2 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago