How to inherit promise, and add new methods for resolve and reject

because normal calls will require repeated writing of a large number of page prompts

class mPromise extends Promise{
    //...

}

then change the original return new Promise to return new mPromise

allows you not to write showLoading () and showfail ()

in the logic of then and catch .

could you tell me how to write it in inheritance?

or how to write it in decorator mode?

Mar.30,2021

this is pseudo requirement .

it doesn't need to be so complicated to achieve such an effect at all!

the following short code can be implemented.

// api
const baseApi = (url, options) => Api(url, options).then(showLoading).catch(err => {
    showFail(err)
    throw err
})
//    
baseApi('url', {}).then(dosomething).catch(dosomething) 

I hope my answer can help you. Thank you.


similar to implementing Promise.finally

Promise.prototype.finally = function (callback) {
  let P = this.constructor
  return this.then(
    value  => P.resolve(callback()).then(() => value),
    reason => P.resolve(callback()).then(() => { throw reason })
  )
}

@ the answer of is very interesting. Let me be an interpreter. Ha, please don't mind, because I'm not familiar with Promise chain myself. So try the following:
see the following figure

clipboard.png

Menu