@kwsites/promise-result
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

@kwsites/promise-result

A utility to transform any promise into its fulfilled value whether it resolved or rejected.

Usage

Capture the fulfillment result of a promise using promiseResult:

import { promiseResult } from '@kwsites/promise-result';

const successValue = 'Good';
const errorValue = new Error('Bad');

expect(await promiseResult(Promise.resolve(successValue)))
  .toEqual(expect.objectContaining({
    result: successValue,
    value: successValue,
    success: true,
    threw: false
  }));

expect(await promiseResult(Promise.reject(errorValue)))
  .toEqual(expect.objectContaining({
    result: errorValue,
    error: errorValue,
    success: false,
    threw: true,
  }));

Capture just the rejection reason from a promise with promiseError:

import { promiseError } from '@kwsites/promise-result';

const successValue = 'Good';
const errorValue = new Error('Bad');

expect(await promiseError(Promise.resolve(successValue)))
    .toBeUndefined();

expect(await promiseError(Promise.reject(errorValue)))
  .toBe(errorValue);

Readme

Keywords

none

Package Sidebar

Install

npm i @kwsites/promise-result

Weekly Downloads

10

Version

1.2.0

License

MIT

Unpacked Size

12.3 kB

Total Files

7

Last publish

Collaborators

  • steveukx