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

1.0.0 • Public • Published

Deferred

Deferred is a simple way to create a promise that can be resolved or rejected at a later time in node-js.

Installation

npm install deferred

or

yarn add deferred

Usage

import { Deferred } from 'deferred';

function doSomethingAsync(): Promise<string> {
  const deferred = new Deferred<string>();

  setTimeout(() => {
    deferred.resolve('Hello World!');
  }, 1000);

  return deferred.promise;
}

function throwSomethingAsync(): Promise<string> {
  const deferred = new Deferred<string, Error>();

  setTimeout(() => {
    deferred.reject(new Error('Something went wrong!'));
  }, 1000);

  return deferred.promise;
}

function main() {
  doSomethingAsync()
    .then((result) => {
      console.log(result);
    })
    .catch((error) => {
      console.error(error);
    });

  throwSomethingAsync()
    .then((result) => {
      console.log(result);
    })
    .catch((error) => {
      console.error(error);
    });
}

Package Sidebar

Install

npm i async-promise-deferred

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

14.9 kB

Total Files

12

Last publish

Collaborators

  • gitlab-az1