Skip to content

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

License

Notifications You must be signed in to change notification settings

Hookyns/PromiseSource

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Promise Completion Source

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

Synopsis

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

Example

import PromiseSource from "promise-cs";

function doSomething(): Promise<number>
{
	let ps = new PromiseSource<number>(/* 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))
;