Yes, just use new CustomPromise and pass the result of then into that new promise.
This is the implementation of promise wrapper in my project (I still have part of the code using prototypes).
functionQuotedPromise(promise){varinternal={pending:true,rejected:false,fulfilled:false,reason:undefined,type:undefined};// then added to __promise__ is needed otherwise rejection// will give UnhandledPromiseRejectionWarning in Node.jspromise=promise.then(v=>{internal.type=type(v);internal.fulfilled=true;internal.pending=false;returnv;});// promise without catch, used for valueOf - for rejecting// that should throw an error when used with awaitread_only(this,'_promise',promise,{hidden:true});if(is_function(promise.catch)){// prevent exception on unhandled rejecting when using// '>(Promise.reject (new Error "zonk")) in REPLpromise=promise.catch((err)=>{internal.rejected=true;internal.pending=false;internal.reason=err;});}Object.keys(internal).forEach(name=>{Object.defineProperty(this,`__${name}__`,{enumerable:true,get:()=>internal[name]});});read_only(this,'__promise__',promise);// prevent resolving when returned from real promise #153this.then=false;}// ----------------------------------------------------------------------QuotedPromise.prototype.then=function(fn){returnnewQuotedPromise(this.valueOf().then(fn));};// ----------------------------------------------------------------------QuotedPromise.prototype.catch=function(fn){returnnewQuotedPromise(this.valueOf().catch(fn));};// ----------------------------------------------------------------------QuotedPromise.prototype.valueOf=function(){if(!this._promise){thrownewError('QuotedPromise: invalid promise created');}returnthis._promise;};// ----------------------------------------------------------------------QuotedPromise.prototype.toString=function(){if(this.__pending__){returnQuotedPromise.pending_str;}if(this.__rejected__){returnQuotedPromise.rejected_str;}return`#<js-promise resolved (${this.__type__})>`;};QuotedPromise.pending_str='#<js-promise (pending)>';QuotedPromise.rejected_str='#<js-promise (rejected)>';
The pattern is very similar to Monads (in fact some explanaions shows promises as example of modals) and Monad should never mutate the data.
Yes, just use new CustomPromise and pass the result of then into that new promise.
This is the implementation of promise wrapper in my project (I still have part of the code using prototypes).
The pattern is very similar to Monads (in fact some explanaions shows promises as example of modals) and Monad should never mutate the data.
Nice, yes I ended up solving it like this:
Though not complete, it does the job and could be improved as the needs arise.
Thanks again for pointing this out, I've made an edit in the post to explain to future readers.
Ohh cool, never thought of promises as monads, makes sense