mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-11-19 06:32:58 +05:30
Wrap fetch in Promise to allow usage of Promise.prototype.finally on promises returned from request service
This commit is contained in:
parent
9b093d1353
commit
45fea4dca7
@ -26,6 +26,12 @@ function buildQuery(data = {}) {
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function doFetch(...args) {
|
||||||
|
// NOTE: we are wrapping fetch, because it is returning
|
||||||
|
// Promise instance that can not be pollyfilled with Promise.prototype.finally
|
||||||
|
return new Promise((resolve, reject) => fetch(...args).then(resolve, reject));
|
||||||
|
}
|
||||||
|
|
||||||
let authToken;
|
let authToken;
|
||||||
|
|
||||||
const checkStatus = (resp) => Promise[resp.status >= 200 && resp.status < 300 ? 'resolve' : 'reject'](resp);
|
const checkStatus = (resp) => Promise[resp.status >= 200 && resp.status < 300 ? 'resolve' : 'reject'](resp);
|
||||||
@ -45,7 +51,7 @@ const getDefaultHeaders = () => {
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
post(url, data) {
|
post(url, data) {
|
||||||
return fetch(url, {
|
return doFetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
...getDefaultHeaders(),
|
...getDefaultHeaders(),
|
||||||
@ -65,7 +71,7 @@ export default {
|
|||||||
url += separator + buildQuery(data);
|
url += separator + buildQuery(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch(url, {
|
return doFetch(url, {
|
||||||
headers: getDefaultHeaders()
|
headers: getDefaultHeaders()
|
||||||
})
|
})
|
||||||
.then(checkStatus)
|
.then(checkStatus)
|
||||||
|
Loading…
Reference in New Issue
Block a user