Socket
Socket
Sign inDemoInstall

async-interactor

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    async-interactor

Interactor pattern using async/await


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

async interactor

Interactor pattern for node.js/browser using async/await

Getting Started

$ npm install --save async-interactor

NOTE: async/await support required from node 7.6.0+ or something like async-to-gen

Usage

import Interactor from 'async-interactor'

class AuthenticateUser extends Interactor {
  async call () {
    const {username, password} = this.context

    const user = await db.where({username, password}).find()

    if (!user) {
      this.fail('User not found')
    }

    this.context.user = user
  }
}

// example route handler
app.post('/login', async (req, res) => {
  const result = await AuthenticateUser.call(req.params)
  if (result.success) {
    res.send({success: true, user: result.user})
  }
})

Organizers

import Interactor from 'async-interactor'

class AddSubscription extends Interactor {
  // return Array of interactors
  organize () {
    return [AuthenticateUser, FinalizePayment]
  }
}

app.post('/buy', async (req, res) => {
  const result = await AddSubscription.call(req.params)
})

Errors

By default any errors thrown inside of an interactor are swallowed and return in the result of the interactor. This allows you to check the result of the interactor after it runs, regardless of a success or failure. There is a throwOnError option available if you don't want this default behavior.

class ThisWillThrow extends Interactor {
  throwOnError = true

  async call () {
    throw new Error('Boom')
  }
}

const result = await ThisWillThrow.call({})
console.log(result) // <- this never runs because the error is `thrown`

Keywords

FAQs

Last updated on 04 May 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc