Api.get(...).then(...).catch(...).finally Is Not A Function
Answer :
I suggest to use another then
instead of using finally
. then
after catch
is works like a finally
. don't forget to use at least one catch
in your promise chain, in order to handle your instructions failure.
So this two line of code is the same:
api.get(…).then(…).catch(…).then(...)
and
api.get(…).then(…).catch(…).finally(...)
Only a native Promise (constructed with new Promise
) is guaranteed to have a .finally
method (in newer environments). (in older environments, .finally
won't work at all with Promises created with new Promise
)
It looks like axios doesn't use new Promise
internally - rather, it just returns a thenable, which is not guaranteed to have a finally
method (and because it doesn't, it throws an error).
While you could use the explicit Promise construction antipattern to wrap the axios call in a native new Promise
, so that it has Promise.prototype.finally
in its prototype chain, a better option (thanks Bergi!) is to just use Promise.resolve
, which will turn a thenable into a native Promise while preserving the failure or success of the thenable:
get(API, params = this.defaultParams) { this.call = "GET"; let constructedURL = this.constructURL(API, params); axiosRetry(axios, { retries: this.retry }); return Promise.resolve(axios.get(constructedURL, this.config)); }
Comments
Post a Comment