manual-promise
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

manual-promise

A drop in replacement for JS Promise that exposes the resolve and reject function to scopes that have access to the promise instance; Plain JS promise can only be resolved or rejected from inside the function passed to the constructor of that promise, ManualPromise allows the promise to be resolved or rejected from outside itself.

Installation

yarn add manual-promise

npn install manual-promise

How

Extends Promise so

new ManualPromise() instanceof Promise === true

Other solutions on the internet use a proxy or wrapper class for promise so they do not function as a drop in replacement.

Use

 
import { ManualPromise } from "manual-promise";
 
const prom = new ManualPromise();
 
prom.resolve(2);
 
// actions can still be run inside the promise
const prom2 = new ManualPromise((resolve, reject) => {
    // ... code
});
 

The class also exists on window, so the import is optional. Add to your global.d.ts file if using typescript and do not wish to import.

View the test file for examples.

Package Sidebar

Install

npm i manual-promise

Weekly Downloads

11

Version

2.0.2

License

MIT

Unpacked Size

33.1 kB

Total Files

7

Last publish

Collaborators

  • zpxp