const TaskStates={FULFILLED:"fulfilled",PENDING:"pending",REJECTED:"rejected"};class Task{constructor(t){this._promise=new Promise(t),this._state="pending"}static attempt(e){return new Task(t=>{t(e())})}static race(a){return new Task((t,e)=>{for(const s of a)"object"==typeof s&&"function"==typeof s.then?s.then(t,e):t(s)})}finally(e){const s=t=>Promise.resolve(e()).then(t);return this.then(t=>s(()=>t),t=>s(()=>Promise.reject(t)))}then(e,s){const a=t=>(this._state="fulfilled",this._value=t,e(t)),r=t=>(this._state="rejected",this._reason=t,s(t));return new Task(t=>t(this._promise.then(a,r)))}isFulfilled(){return this._state===TaskStates.FULFILLED}isPending(){return this._state===TaskStates.PENDING}isRejected(){return this._state===TaskStates.REJECTED}isResolved(){return this._state!==TaskStates.PENDING}reason(){if(this._state!==TaskStates.REJECTED)throw new Error(`Attempting to read failure reason from a Task that is not rejected. This Task is ${this._state}.`);return this._reason}value(){if(this._state!==TaskStates.FULFILLED)throw new Error(`Attempting to read value from a Task that is not resolved. This Task is ${this._state}.`);return this._value}}export{Task};