# promise-interactor

> Promise wrapped interactor pattern for NodeJS

Latest version **2.2.8** (published 2022-07-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install promise-interactor
pnpm add promise-interactor
yarn add promise-interactor
bun add promise-interactor
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.8 |
| Published | 2022-07-10 |
| First published | 2017-03-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 27.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | June Sapara |
| Maintainers | interlock |
| Keywords | promise, interactor |

## Links

- npm: https://www.npmjs.com/package/promise-interactor
- Repository: https://github.com/interlock/promise-interactor
- Homepage: https://interlock.github.io/promise-interactor
- Issues: https://github.com/interlock/promise-interactor/issues
- npm.io page: https://npm.io/package/promise-interactor

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 2.2.8 (latest) — 2022-07-10
- 2.2.7 — 2020-08-14
- 2.2.5 — 2020-08-14
- 2.2.4 — 2020-08-13
- 2.2.3 — 2020-08-11
- 2.2.2 — 2020-08-11
- 2.2.1 — 2020-07-23
- 2.2.0 — 2020-07-21
- 2.1.0 — 2019-12-06
- 2.0.2 — 2019-03-13
- 2.0.0 — 2018-08-15
- 2.0.0-PRE2 — 2018-08-15
- 2.0.0-PRE1 — 2018-08-15
- 1.3.0 — 2018-04-13
- 1.2.1 — 2017-06-26
- … 14 more at https://npm.io/package/promise-interactor/versions

## README

![Node.js CI](https://github.com/interlock/promise-interactor/workflows/Node.js%20CI/badge.svg)
[![Dependency Status](https://david-dm.org/interlock/promise-interactor.svg)](https://david-dm.org/interlock/promise-interactor)
[![Dev Dependency Status](https://david-dm.org/interlock/promise-interactor/dev-status.svg)](https://david-dm.org/interlock/promise-interactor/dev-status)
[![Greenkeeper badge](https://badges.greenkeeper.io/interlock/promise-interactor.svg)](https://greenkeeper.io/)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Finterlock%2Fpromise-interactor.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Finterlock%2Fpromise-interactor?ref=badge_shield)

# Interactors

Based on the popular ruby gem [Interactor](https://github.com/collectiveidea/interactor). Uses chainable promises to allow
flow of completing multiple interactors.


## Single Pattern

```js
import PromiseInteractor from 'promise-interactor';

const { Interactor } = PromiseInteractor;

class AuthenticateUser extends Interactor {

  // optional before, if it returns a promise it is inserted in the promise chain
  before() {
    return Promise.resolve(true);
  }

  call() {
    const { password, email } = this.context;
    User.find({email: email}).then((user) => {
      if (user.password === password) {
        this.context.user = user;
        this.resolve();
      } else {
        this.reject(new Error("Invalid password"));
      }
    }).error( (err) => {
      this.reject(err);
    });
  }

  // optional after, if it returns a promise it is inserted in the promise chain
  // just before we resolve the root promise
  after() {
    return Promise.resolve(true);
  }
}

module.exports = AuthenticateUser;
```

Calling the interactor

```js
(new AuthenticateUser({email, password}))
  .exec()
  .then((interactor) => {
    console.log(`User logged in: ${interactor.context.user}`);
  })
  .catch((err) => {
    console.log(`Error: ${error}`);
  });

```

## Rollback

If your interactor rejected, you can optionally provide a rollback which will clean up

```js
class AuthenticateUser extends Interactor{
  // If we returned a promise, it would wait on that before calling the reject
  // otherwise this is considered a sync call and the promise is rejected immediately after
  rollback(err) {
    console.log(err);
  }
}
```

## Organization Pattern

Makes grouping interactors in sequence a little easier.

```js
import PromiseInteractor from 'promise-interactor';

const { Organizer } = PromiseInteractor;

class AuthUserOrganizer extends Organizer {

  organize() {
    return [AuthenticateUser, SomeOtherInteractor];
  }
}

AuthUserOrganizer
  .exec({email, password})
  .then((i) => {
    console.log(i.context);
  });
...
```

`rollback` for Organizer will call `rollback` for previously RESOLVED interactors with the current state. You can override this functionality by implementing your own `rollback` on Organizer.


## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Finterlock%2Fpromise-interactor.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Finterlock%2Fpromise-interactor?ref=badge_large)

---
_Source: https://npm.io/package/promise-interactor · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
