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

1.0.0 • Public • Published

promise-completer

A helper to resolve/reject your promise whenerver you want.

Motivation

TL;DR

Because I hate define multiple variables when I can not use util.promisify callbacks..

Long story

Quite often we want to promisfy callbacks. If they adhere to the Node.js (err, ...args) => returnVal signature require('util').promisify is enough.

But sometimes, it is not possible. And we end up writing something like the example below:

let shutdownResolve; /* var 1 */
let shutdownReject; /* var 2 */
const shutdownPromise /* and a constant */ = new Promise((resolve, reject) => { 
  shutdownResolve = resolve;
  shutdownReject = reject; 
});
 
function asyncComputation() {
  canNotPromisfyThisFunc (function myCallback () {
    ....
    shutdownResolve();
    ....
    shutdownReject(error);
    ....
  })
}
 
await shutdownPromise;

Without compromise on type checking. This package allows you to write the code.

var Completer = require('node-completer');
const shutdown = new Completer;
 
function asyncComputation() {
  canNotPromisfyThisFunc (function myCallback () {
    ....
    shutdown.resolve();
    ....
    shutdown.reject(error);
    ....
  })
}
 
await shutdown.promise;

Readme

Keywords

Package Sidebar

Install

npm i promise-completer

Weekly Downloads

67

Version

1.0.0

License

MIT

Unpacked Size

2.89 kB

Total Files

4

Last publish

Collaborators

  • motia