async-promise-deferred
v1.0.0
Published
Deferred is a simple way to create a promise that can be resolved or rejected at a later time in [node-js](https://https://nodejs.org/).
Downloads
2
Maintainers
Readme
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);
});
}