mutable-promise
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -26,5 +26,5 @@ /*! ***************************************************************************** | ||
const isPromiseLike = (value) => (value && "object" === typeof value && "function" === typeof value.then); | ||
class MutablePromise extends Promise { | ||
constructor(executor) { | ||
const promise = new Promise(executor || (() => { })); | ||
let rs; | ||
@@ -35,11 +35,15 @@ let rj; | ||
this._s = "pending"; | ||
this.innerPromise = promise; | ||
this.resolve = (value) => { | ||
if ("pending" === this._s) { | ||
this._s = "fulfilled"; | ||
rs(value); | ||
if (this.isPending) { | ||
if (isPromiseLike(value)) { | ||
this.task = value; | ||
} | ||
else { | ||
this._s = "fulfilled"; | ||
rs(value); | ||
} | ||
} | ||
}; | ||
this.reject = (reason) => { | ||
if ("pending" === this._s) { | ||
if (this.isPending) { | ||
this._s = "rejected"; | ||
@@ -49,20 +53,36 @@ rj(reason); | ||
}; | ||
this.task = executor; | ||
} | ||
get status() { return this._s; } | ||
get innerPromise() { return this._p; } | ||
set innerPromise(value) { | ||
this._p = value; | ||
(() => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const ret = yield value; | ||
if (value === this._p) { | ||
this.resolve(ret); | ||
get isPending() { return "pending" === this._s; } | ||
get isFulfilled() { return "fulfilled" === this._s; } | ||
get isRejected() { return "rejected" === this._s; } | ||
get task() { return this._task; } | ||
set task(value) { | ||
//if(!this.isPending){ return; } | ||
this._task = value; | ||
let p; | ||
if (isPromiseLike(value)) { | ||
p = value; | ||
} | ||
else if ("function" === typeof value) { | ||
p = new Promise(value); | ||
} | ||
if (p) { | ||
(() => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const ret = yield p; | ||
// make sure task not change | ||
if (value === this._task) { | ||
this.resolve(ret); | ||
} | ||
} | ||
} | ||
catch (reason) { | ||
if (value === this._p) { | ||
this.reject(reason); | ||
catch (reason) { | ||
// make sure task not change | ||
if (value === this._task) { | ||
this.reject(reason); | ||
} | ||
} | ||
} | ||
}))(); | ||
}))(); | ||
} | ||
} | ||
@@ -69,0 +89,0 @@ } |
@@ -1,2 +0,2 @@ | ||
function t(t,e,n,s){return new(n||(n=Promise))((function(i,r){function o(t){try{h(s.next(t))}catch(t){r(t)}}function c(t){try{h(s.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(o,c)}h((s=s.apply(t,e||[])).next())}))}class e extends Promise{constructor(t){const e=new Promise(t||(()=>{}));let n,s;super(((t,e)=>{n=t,s=e})),this._s="pending",this.innerPromise=e,this.resolve=t=>{"pending"===this._s&&(this._s="fulfilled",n(t))},this.reject=t=>{"pending"===this._s&&(this._s="rejected",s(t))}}get status(){return this._s}get innerPromise(){return this._p}set innerPromise(e){this._p=e,(()=>{t(this,void 0,void 0,(function*(){try{const t=yield e;e===this._p&&this.resolve(t)}catch(t){e===this._p&&this.reject(t)}}))})()}}export{e as default}; | ||
function t(t,e,s,i){return new(s||(s=Promise))((function(n,r){function o(t){try{h(i.next(t))}catch(t){r(t)}}function c(t){try{h(i.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?n(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(o,c)}h((i=i.apply(t,e||[])).next())}))}const e=t=>t&&"object"==typeof t&&"function"==typeof t.then;class s extends Promise{constructor(t){let s,i;super(((t,e)=>{s=t,i=e})),this._s="pending",this.resolve=t=>{this.isPending&&(e(t)?this.task=t:(this._s="fulfilled",s(t)))},this.reject=t=>{this.isPending&&(this._s="rejected",i(t))},this.task=t}get status(){return this._s}get isPending(){return"pending"===this._s}get isFulfilled(){return"fulfilled"===this._s}get isRejected(){return"rejected"===this._s}get task(){return this._task}set task(s){let i;this._task=s,e(s)?i=s:"function"==typeof s&&(i=new Promise(s)),i&&(()=>{t(this,void 0,void 0,(function*(){try{const t=yield i;s===this._task&&this.resolve(t)}catch(t){s===this._task&&this.reject(t)}}))})()}}export{s as default}; | ||
//# sourceMappingURL=mutable-promise.min.mjs.map |
@@ -32,5 +32,5 @@ (function (global, factory) { | ||
const isPromiseLike = (value) => (value && "object" === typeof value && "function" === typeof value.then); | ||
class MutablePromise extends Promise { | ||
constructor(executor) { | ||
const promise = new Promise(executor || (() => { })); | ||
let rs; | ||
@@ -41,11 +41,15 @@ let rj; | ||
this._s = "pending"; | ||
this.innerPromise = promise; | ||
this.resolve = (value) => { | ||
if ("pending" === this._s) { | ||
this._s = "fulfilled"; | ||
rs(value); | ||
if (this.isPending) { | ||
if (isPromiseLike(value)) { | ||
this.task = value; | ||
} | ||
else { | ||
this._s = "fulfilled"; | ||
rs(value); | ||
} | ||
} | ||
}; | ||
this.reject = (reason) => { | ||
if ("pending" === this._s) { | ||
if (this.isPending) { | ||
this._s = "rejected"; | ||
@@ -55,20 +59,36 @@ rj(reason); | ||
}; | ||
this.task = executor; | ||
} | ||
get status() { return this._s; } | ||
get innerPromise() { return this._p; } | ||
set innerPromise(value) { | ||
this._p = value; | ||
(() => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const ret = yield value; | ||
if (value === this._p) { | ||
this.resolve(ret); | ||
get isPending() { return "pending" === this._s; } | ||
get isFulfilled() { return "fulfilled" === this._s; } | ||
get isRejected() { return "rejected" === this._s; } | ||
get task() { return this._task; } | ||
set task(value) { | ||
//if(!this.isPending){ return; } | ||
this._task = value; | ||
let p; | ||
if (isPromiseLike(value)) { | ||
p = value; | ||
} | ||
else if ("function" === typeof value) { | ||
p = new Promise(value); | ||
} | ||
if (p) { | ||
(() => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const ret = yield p; | ||
// make sure task not change | ||
if (value === this._task) { | ||
this.resolve(ret); | ||
} | ||
} | ||
} | ||
catch (reason) { | ||
if (value === this._p) { | ||
this.reject(reason); | ||
catch (reason) { | ||
// make sure task not change | ||
if (value === this._task) { | ||
this.reject(reason); | ||
} | ||
} | ||
} | ||
}))(); | ||
}))(); | ||
} | ||
} | ||
@@ -75,0 +95,0 @@ } |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).MutablePromise=t()}(this,(function(){"use strict";function e(e,t,n,i){return new(n||(n=Promise))((function(s,o){function r(e){try{u(i.next(e))}catch(e){o(e)}}function c(e){try{u(i.throw(e))}catch(e){o(e)}}function u(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,c)}u((i=i.apply(e,t||[])).next())}))}return class extends Promise{constructor(e){const t=new Promise(e||(()=>{}));let n,i;super(((e,t)=>{n=e,i=t})),this._s="pending",this.innerPromise=t,this.resolve=e=>{"pending"===this._s&&(this._s="fulfilled",n(e))},this.reject=e=>{"pending"===this._s&&(this._s="rejected",i(e))}}get status(){return this._s}get innerPromise(){return this._p}set innerPromise(t){this._p=t,(()=>{e(this,void 0,void 0,(function*(){try{const e=yield t;t===this._p&&this.resolve(e)}catch(e){t===this._p&&this.reject(e)}}))})()}}})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).MutablePromise=e()}(this,(function(){"use strict";function t(t,e,s,i){return new(s||(s=Promise))((function(n,o){function r(t){try{c(i.next(t))}catch(t){o(t)}}function u(t){try{c(i.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?n(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(r,u)}c((i=i.apply(t,e||[])).next())}))}const e=t=>t&&"object"==typeof t&&"function"==typeof t.then;return class extends Promise{constructor(t){let s,i;super(((t,e)=>{s=t,i=e})),this._s="pending",this.resolve=t=>{this.isPending&&(e(t)?this.task=t:(this._s="fulfilled",s(t)))},this.reject=t=>{this.isPending&&(this._s="rejected",i(t))},this.task=t}get status(){return this._s}get isPending(){return"pending"===this._s}get isFulfilled(){return"fulfilled"===this._s}get isRejected(){return"rejected"===this._s}get task(){return this._task}set task(s){let i;this._task=s,e(s)?i=s:"function"==typeof s&&(i=new Promise(s)),i&&(()=>{t(this,void 0,void 0,(function*(){try{const t=yield i;s===this._task&&this.resolve(t)}catch(t){s===this._task&&this.reject(t)}}))})()}}})); | ||
//# sourceMappingURL=mutable-promise.min.js.map |
declare type ResolveValue<T> = T | PromiseLike<T>; | ||
declare type Resolve<T> = (value: ResolveValue<T>) => void; | ||
declare type Reject = (reason?: any) => void; | ||
declare type Executor<T> = (resolve: Resolve<T>, reject: Reject) => void; | ||
declare class MutablePromise<T> extends Promise<T> { | ||
private _p; | ||
private _s; | ||
get status(): string; | ||
get innerPromise(): Promise<T>; | ||
set innerPromise(value: Promise<T>); | ||
get isPending(): boolean; | ||
get isFulfilled(): boolean; | ||
get isRejected(): boolean; | ||
private _task; | ||
get task(): PromiseLike<T> | Executor<T> | null; | ||
set task(value: PromiseLike<T> | Executor<T> | null); | ||
resolve: Resolve<T>; | ||
reject: Reject; | ||
constructor(executor?: (resolve: Resolve<T>, reject: Reject) => void); | ||
constructor(executor?: PromiseLike<T> | Executor<T> | null); | ||
} | ||
export default MutablePromise; | ||
export { Resolve, Reject }; | ||
export { Resolve, Reject, ResolveValue, Executor }; | ||
//# sourceMappingURL=index.d.ts.map |
{ | ||
"name": "mutable-promise", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Wrapper for Promise. Resolvable, rejectable, redirectable.", | ||
@@ -5,0 +5,0 @@ "files": [ |
151
README.md
# MutablePromise | ||
Wrapper for Promise. Resolvable, rejectable, redirectable. | ||
<br> | ||
## Import | ||
<br> | ||
install | ||
@@ -14,2 +19,4 @@ ``` | ||
<br> | ||
import | ||
@@ -23,10 +30,12 @@ ```mjs | ||
<br> | ||
CDN | ||
```html | ||
<script type="module"> | ||
import MutablePromise from 'https://cdn.jsdelivr.net/npm/mutable-promise@1.0.0/dist/mutable-promise.esm.min.js'; | ||
import MutablePromise from 'https://cdn.jsdelivr.net/npm/mutable-promise@1.1.0/dist/mutable-promise.esm.min.js'; | ||
</script> | ||
``` | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/mutable-promise@1.0.0/dist/mutable-promise.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/mutable-promise@1.1.0/dist/mutable-promise.min.js"></script> | ||
``` | ||
@@ -36,26 +45,136 @@ | ||
<br> | ||
`resolve` outside of the executor. | ||
Get `status` of the `promise`. | ||
```js | ||
let p; | ||
// can use like an native `Promise` | ||
p = new MutablePromise((resolve, reject)=>setTimeout(reject,1000)); | ||
// can use like a native `Promise` | ||
let p = new MutablePromise((_, rj)=>setTimeout(rj,100)); // try to reject after 100ms | ||
// can get status | ||
console.log(p.status); // "pending" | ||
console.log(p.isPending); // true | ||
// can resolve outside | ||
p.then(anything=>console.log(anything)); // 'resolve called' | ||
p.resolve('resolve called'); | ||
p.resolve('resolve called'); // resolve immediately | ||
console.log(p.status); // "fulfilled" | ||
p = new MutablePromise(resolve=>setTimeout(resolve,1000)); | ||
console.log(p.isFulfilled); // true | ||
(async()=>{ | ||
await new Promise(rs=>setTimeout(rs,200)); // wait 200ms | ||
// status will not change after `fulfilled` or `rejected` | ||
console.log(p.status); // "fulfilled" | ||
console.log(p.isFulfilled); // true | ||
})(); | ||
``` | ||
<br> | ||
`reject` outside of the executor. | ||
```js | ||
let p = new MutablePromise(rs=>setTimeout(rs,100)); // try to resolve after 100ms | ||
// can reject outside | ||
p.catch(anything=>console.log(anything)); // 'reject called' | ||
p.reject('reject called'); | ||
p.reject('reject called'); // reject immediately | ||
console.log(p.status); // "rejected" | ||
// Allow setting no arguments, define a `innerPromise` later | ||
p = new MutablePromise(); | ||
p.then(anything=>console.log(anything)); | ||
p.innerPromise = new Promise(resolve=>resolve('msg from later define innerPromise')); | ||
// Can change task by change `innerPromise` | ||
p = new MutablePromise(resolve=>setTimeout(()=>{resolve('original task')},1000)); | ||
console.log(p.isRejected); // true | ||
``` | ||
<br> | ||
Allow setting a `promise` as argument. | ||
```js | ||
// simple promise | ||
new MutablePromise(Promise.resolve('1')); | ||
// typical promise | ||
let nativeP = new Promise(rs=>setTimeout(rs,200)); | ||
new MutablePromise(nativeP); | ||
// wrapper a fetch | ||
new MutablePromise(fetch('./')); | ||
// nested MutablePromise | ||
let p = new MutablePromise(rs=>setTimeout(rs,200)); | ||
new MutablePromise(p); | ||
// promise like | ||
let pLike = { then: function(){ return 'a'; } }; | ||
new MutablePromise(pLike); | ||
``` | ||
<br> | ||
Allow setting no argument or `null`. Then define a `task` later. | ||
```js | ||
let p = new MutablePromise(); // or `new MutablePromise(null)` | ||
p.then(anything=>console.log(anything)); // 'msg from later define task' | ||
// The property `task` can accept the same parameter type as the constructor of `MutablePromise` | ||
p.task = rs=>rs('msg from later define task'); | ||
``` | ||
<br> | ||
Can change `task` before `fulfilled` or `rejected`. | ||
```js | ||
let p = new MutablePromise(rs=>setTimeout(()=>{rs('original task')},100)); | ||
p.then(anything=>console.log(anything)); // 'new task' | ||
// Can also use a `MutablePromise` as `innerPromise` | ||
p.innerPromise = new MutablePromise(resolve=>setTimeout(()=>{resolve('new task')},2000)); | ||
p.task = new MutablePromise(rs=>setTimeout(()=>{rs('new task')},200)); | ||
``` | ||
<br> | ||
A change make no sense after `fulfilled` or `rejected`. | ||
```js | ||
let p = new MutablePromise(Promise.resolve('resolved')); | ||
p.then(anything=>console.log(anything)); // 'resolved' | ||
(async()=>{ | ||
// wait next js event loop | ||
await new Promise(rs=>setTimeout(rs,0)); | ||
console.log(p.status); // "fulfilled" | ||
p.catch(anything=>console.log(anything)); // will not log anything | ||
p.task = Promise.reject('make no sense'); | ||
await new Promise(rs=>setTimeout(rs,0)); | ||
p.task = (rs)=>{ | ||
console.log('function still run after `fulfilled` or `rejected`'); | ||
rs('but will not resolve or reject'); | ||
}; | ||
})(); | ||
``` | ||
<br> | ||
Set `task` as `null` can cancel the orignial task. | ||
```js | ||
let p = new MutablePromise(resolve=>setTimeout(()=>{ | ||
console.log('the executor will run anyway'); | ||
resolve('original task'); | ||
},100)); | ||
p.then(anything=>console.log(anything)); // will not log anything | ||
p.task = null; | ||
// the promise will keep `pending` | ||
setTimeout(()=>{ | ||
console.log(p.status); | ||
},200) | ||
// you can define a new `task` later | ||
``` | ||
<br> | ||
If a `MutablePromise` has been `fulfilled` or `rejected`, you can define a new `MutablePromise` instead. | ||
```js | ||
(async()=>{ | ||
let p = new MutablePromise(Promise.resolve()); | ||
await new Promise(rs=>setTimeout(rs,0)); // wait next js event loop | ||
console.log(p.status); // "fulfilled" | ||
p = new MutablePromise(resolve=>setTimeout(()=>{resolve('you can define a new `MutablePromise` instead')},100)); | ||
p.then(anything=>console.log(anything)); | ||
})(); | ||
``` | ||
## TODO | ||
Maybe need to wrap other promise function like `all`, `resolve`, `race` and so on, to `MutablePromise` version. |
type ResolveValue<T> = T | PromiseLike<T>; | ||
type Resolve<T> = (value: ResolveValue<T>) => void; | ||
type Reject = (reason?: any) => void; | ||
type Executor<T> = (resolve: Resolve<T>, reject: Reject) => void; | ||
const isPromiseLike = (value:any) => (value && "object" === typeof value && "function" === typeof value.then); | ||
class MutablePromise<T> extends Promise<T>{ | ||
private _p: Promise<T>; | ||
private _s: string; | ||
get status(){ return this._s; } | ||
get innerPromise(){ return this._p; } | ||
set innerPromise(value: Promise<T>){ | ||
this._p = value; | ||
(async()=>{ | ||
try{ | ||
const ret = await value; | ||
if(value === this._p){ | ||
this.resolve(ret); | ||
get isPending(){ return "pending" === this._s; } | ||
get isFulfilled(){ return "fulfilled" === this._s; } | ||
get isRejected(){ return "rejected" === this._s; } | ||
private _task: PromiseLike<T> | Executor<T> | null; | ||
get task(){ return this._task; } | ||
set task(value: PromiseLike<T> | Executor<T> | null){ | ||
//if(!this.isPending){ return; } | ||
this._task = value; | ||
let p: PromiseLike<T>; | ||
if(isPromiseLike(value)){ | ||
p = value as PromiseLike<T>; | ||
}else if("function" === typeof value){ | ||
p = new Promise(value); | ||
} | ||
if(p){ | ||
(async()=>{ | ||
try{ | ||
const ret = await p; | ||
// make sure task not change | ||
if(value === this._task){ | ||
this.resolve(ret); | ||
} | ||
}catch(reason){ | ||
// make sure task not change | ||
if(value === this._task){ | ||
this.reject(reason); | ||
} | ||
} | ||
}catch(reason){ | ||
if(value === this._p){ | ||
this.reject(reason); | ||
} | ||
} | ||
})(); | ||
})(); | ||
} | ||
} | ||
resolve: Resolve<T> | ||
reject: Reject; | ||
constructor(executor?: (resolve: Resolve<T>, reject: Reject) => void){ | ||
const promise = new Promise<T>(executor || (()=>{})); | ||
constructor(executor?: PromiseLike<T> | Executor<T> | null){ | ||
let rs: Resolve<T>; | ||
@@ -34,11 +56,14 @@ let rj: Reject; | ||
this._s = "pending"; | ||
this.innerPromise = promise; | ||
this.resolve = (value: ResolveValue<T>)=>{ | ||
if("pending" === this._s){ | ||
this._s = "fulfilled"; | ||
rs(value); | ||
if(this.isPending){ | ||
if(isPromiseLike(value)){ | ||
this.task = value as PromiseLike<T>; | ||
}else{ | ||
this._s = "fulfilled"; | ||
rs(value); | ||
} | ||
} | ||
}; | ||
this.reject = (reason?: any)=>{ | ||
if("pending" === this._s){ | ||
if(this.isPending){ | ||
this._s = "rejected"; | ||
@@ -48,2 +73,3 @@ rj(reason); | ||
}; | ||
this.task = executor; | ||
} | ||
@@ -53,2 +79,2 @@ } | ||
export default MutablePromise; | ||
export { Resolve, Reject }; | ||
export { Resolve, Reject, ResolveValue, Executor }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84895
361
178