projects/wms-framework/src/lib/baseframework/Task.ts
Task class
Properties |
Methods |
Accessors |
constructor()
|
Private _exception |
Type : Exception
|
Default value : new Exception()
|
Private _reject |
Type : Function
|
Private _resolve |
Type : Function
|
Private fate |
Type : "resolved" | "unresolved"
|
Public promise |
Type : Promise<T>
|
Private state |
Type : "pending" | "fulfilled" | "rejected"
|
isFulfilled |
isFulfilled()
|
Returns :
boolean
|
isPending |
isPending()
|
Returns :
boolean
|
isRejected |
isRejected()
|
Returns :
boolean
|
isResolved |
isResolved()
|
Returns :
boolean
|
reject | ||||||
reject(reason?: any)
|
||||||
Parameters :
Returns :
void
|
resolve | ||||||
resolve(value?: any)
|
||||||
Parameters :
Returns :
void
|
Exception |
getException()
|
import { Exception } from './Exceptions';
/**
* Task class
*
* @export
* @class Task
* @template T
* @wType System.Threading.Tasks.Task`1
* @wNetSupport
*/
export class Task<T> {
public promise: Promise<T>;
private fate: 'resolved' | 'unresolved';
private state: 'pending' | 'fulfilled' | 'rejected';
private _resolve: Function;
private _reject: Function;
private _exception: Exception = new Exception();
constructor() {
this.state = 'pending';
this.fate = 'unresolved';
this.promise = new Promise((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
});
this.promise.then(
() => (this.state = 'fulfilled'),
(reason?: any) => {
this._exception = reason?.Message
? new Exception(reason?.Message)
: new Exception('Unknown');
this.state = 'rejected';
}
);
}
resolve(value?: any) {
if (this.fate === 'resolved') {
throw 'Deferred cannot be resolved twice';
}
this.fate = 'resolved';
this._resolve(value);
}
reject(reason?: any) {
if (this.fate === 'resolved') {
throw 'Deferred cannot be resolved twice';
}
this.fate = 'resolved';
this._exception = reason?.Message
? new Exception(reason?.Message)
: new Exception('Unknown');
this._reject(reason);
}
isResolved() {
return this.fate === 'resolved';
}
isPending() {
return this.state === 'pending';
}
isFulfilled() {
return this.state === 'fulfilled';
}
isRejected() {
return this.state === 'rejected';
}
public get Exception(): Exception {
return this._exception;
}
}