Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

inside-out-promise

qiwi18MIT2.1.5TypeScript support: included

Produces extended Promises with attached chainable resolvers

promise, extended promise, inside-out-promise, InsideOutPromise, promise status, interted promise, observable promise, esimorp, completable future, CompletableFuture

readme

inside-out-promise

Produces promises with chainable resolvers and observable state.

Build Status David Maintainability Test Coverage npm

Install

npm add inside-out-promise
yarn add inside-out-promise

Key features

  • Chainable resolve and reject methods
  • Exposed promise state and result fields
  • Configurable Promise implementation
  • TS and Flow typings out of box

Usage

import {factory} from 'inside-out-promise'

const promise = factory() // produces a promise
promise.then((data) => {  // standard `thenable` iface
  doSomething(data)
})

const data = await fetch({...})
promise.resolve(data)     // internal resolver is exposed as public promise field
promise.result            // data ref
promise.value             // same data ref, result alias
promise.state             // 'Fulfilled'
promise.status            // status alias: 'Fulfilled'
promise.isPending()       // false
promise.isFulfilled()     // true
promise.isResolved()      // true

// Or the same in OOP style
import {InsideOutPromise} from 'inside-out-promise'
const p = new InsideOutPromise()

API

Resolvers

Both executor args — resolve and reject — are available as chainable public methods:

const promise = factory()

promise.resolve('foo')            // This's a promise
promise.reject(new Error('bar'))  // and this is too

Chains

factory().resolve('value').then(data => {
  // here's the value: data === 'value'
})

factory().catch(error => {
  // the error goes here
}).reject(new Error())

You're able to combine steps in Java-like style: first build the "CompletableFuture" chain and resolve it after

const p = new InsideOutPromise()
  .then(data => data.repeat(2))
    .then(data => data.toUpperCase())
      .then(data => data + 'bar')
        .resolve('foo')

const res = await p // 'FOOFOObar'

Each step return InsideOutPromise instance inherited from Promise, Bluebird (see Configuration for details), etc, so the intanceof check still works.

const p1 = new InsideOutPromise()
const p2 = p1.then(data => data)

const v1 = await(p1)
const v2 = await(p2)

expect(p1).toBeInstanceOf(Promise)
expect(p2).toBeInstanceOf(Promise)
expect(p1).toBeInstanceOf(InsideOutPromise)
expect(p2).toBeInstanceOf(InsideOutPromise)

State

Promise state field may take values: Pending, Fulfilled and Rejected

const promise = factory()
promise.state // 'Pending'

promise.resolve()
promise.state // 'Fulfilled'

There're also 3 helper methods:

  • isPending()
  • isRejected()
  • isFulfilled()

InsideOutPromise

import InsideOutPromise from 'inside-out-promise'

const promise = new InsideOutPromise((resolve, reject) => {
  // Legacy executor flow is still supported
  // ...
})

promise.resolve('foo')
promise.then(data => console.log(data)) // It's `foo`

Configuration

import factory from 'inside-out-promise'
import * as Bluebird from 'bluebird'

factory.Promise = Bluebird // Native `Promise` by default

Refs

changelog

2.1.5 (2023-03-21)

Performance Improvements

2.1.4 (2021-09-15)

Bug Fixes

  • package: up deps, fix vuls (34459a0)

2.1.3 (2021-05-09)

Bug Fixes

  • pkg: up dev deps, fix vuls (32601bb)

2.1.2 (2020-10-30)

Performance Improvements

  • package: up deps, fix vuls (000b286)

2.1.1 (2020-10-09)

Performance Improvements

2.1.0 (2020-06-19)

Features

  • autobind resolve and reject methods (1085156), closes #13
  • let resolve/reject params be optional (4727d29)

2.0.0 (2020-05-16)

Performance Improvements

BREAKING CHANGES

  • package: tslib 2.0 requires TS 3.9+

1.4.3 (2019-12-15)

Performance Improvements

  • package: up deps & repack (1cd25d5)

1.4.2 (2019-11-06)

Bug Fixes

  • typing updates & fixes (4549aa9)
  • up deps, fix TS 3.7 typings (376a145)

1.4.1 (2019-09-15)

Bug Fixes

  • expose constructor name getters (ca70b92)
  • make compatible with Promise<T> (220fb70), closes #12

1.4.0 (2019-09-13)

Features

  • assign promise rejection to reason field (2663d11), closes #5

1.3.1 (2019-09-09)

Bug Fixes

  • correct value and status resolution for descendant promises (657289b)

1.3.0 (2019-09-09)

Features

  • inherit original promise chain (406dc0c)

1.2.0 (2019-09-08)

Features

  • factory: modify proto chain on Promise impl change (13e79e9)

1.1.0 (2019-09-08)

Features

  • add finally method (0a174bb)
  • factory: add options support (7d49ef7), closes #4
  • add aliases for state (status) and result (value) (b93045b)
  • add isResolved method (fe3ca93), closes #3 #2
  • expose promise result as public field (4481bc3)

1.0.0 (2019-09-07)

Features

  • expose promise state field (11da9f3), closes #1
  • factory: add configurable Promise impl (a683ce8)
  • return promise ref as a result of resolve, reject invocations (eafbb83)