Quickly apply the data request interface, how to use finally?

problem description

in fast application development, use Promise method. In view of the complete situation, using finally does not work properly. Consult some materials to know that the fast application official specification does not support finally, so how to solve this problem?

related codes

import fetch from "@system.fetch"

fetch.fetch({
  url: params.url,
  method: params.method,
  data: params.data
})
.then(response => {
  // ....
})
.catch((error, code) => {
  console.log(`request fail, code = ${code}`)
})
.finally(() => {
  // ?
})

did not add / es-shims/Promise.prototype.finally


for cases where finally cannot be used, it can be solved by polyfill, which is also applicable in fast applications. There are many existing methods, and a relatively simple one is introduced. The code is as follows:


Promiseresolvereject2

function fetchData(){
    fetch.fetch({
      url: params.url,
      method: params.method,
      data: params.data
    })
    .then(response => {
      // success
      return Promise.resolve(response);
    })
    .catch((error, code) => {
      console.log(`request fail, code = ${code}`);
      // err
      return Promise.reject(error);
    })
}

then call to execute the code you want in finally regardless of success or failure

fetchData().then(res => {
    // when success
    // do final
}).catch(err => {
    // when error
    // do final
})
Menu