Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hookyns/promisesource

Promise manageable from outer scope, similar to C#'s TaskCompletionSource
https://github.com/hookyns/promisesource

completion javascript nodejs promise source taskcompletionsource typescript

Last synced: about 2 months ago
JSON representation

Promise manageable from outer scope, similar to C#'s TaskCompletionSource

Awesome Lists containing this project

README

        

# Promise Completion Source
Promise manageable from outer scope, similar to C#'s TaskCompletionSource

## Synopsis
```typescript
class PromiseSource {
get promise(): Promise;
get resolved(): boolean;
get rejected(): boolean;
get completed(): boolean;
constructor(timeout?: number);
reject(reason?: any): void;
resolve(value?: TResult): void;
}
```

## Example
```typescript
import PromiseSource from "promise-cs";

function doSomething(): Promise
{
let ps = new PromiseSource(/* optional timeout in millisecond */);

// Some async action, event or something
someAsyncAction((err, result) => {
if (err) {
ps.reject(err);
return;
}

ps.resolve(result);
});

return ps.promise;
}

doSomething()
.then(num => console.log(num))
.catch(reason => console.error(reason))
;
```