refactor promisedRequest to use async/await
This commit is contained in:
parent
82c3fa0bd1
commit
04a0b0b8a8
1 changed files with 33 additions and 39 deletions
|
|
@ -66,7 +66,7 @@ export const paramsString = (params = {}) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const promisedRequest = ({
|
export const promisedRequest = async ({
|
||||||
method,
|
method,
|
||||||
url,
|
url,
|
||||||
params,
|
params,
|
||||||
|
|
@ -107,47 +107,41 @@ export const promisedRequest = ({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch(url, options).then((response) => {
|
const response = await fetch(url, options)
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
// 204 is "No content", which fails to parse json (as you'd might think)
|
// 204 is "No content", which fails to parse json (as you'd might think)
|
||||||
if (response.ok && response.status === 204) resolve()
|
if (response.ok && response.status === 204) return { _response: response }
|
||||||
|
|
||||||
return response
|
|
||||||
.json()
|
|
||||||
.then((json) => {
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
return reject(
|
throw new StatusCodeError(
|
||||||
new StatusCodeError(
|
|
||||||
response.status,
|
response.status,
|
||||||
json,
|
json,
|
||||||
{ url, options },
|
{ url, options },
|
||||||
response,
|
response,
|
||||||
),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const json = await response.json()
|
||||||
|
|
||||||
if (typeof json !== 'object') {
|
if (typeof json !== 'object') {
|
||||||
return resolve({
|
return {
|
||||||
_response: response,
|
_response: response,
|
||||||
_value: json,
|
_value: json,
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
json._response = response
|
json._response = response
|
||||||
|
|
||||||
return resolve(json)
|
return json
|
||||||
})
|
} catch (error) {
|
||||||
.catch((error) => {
|
throw new StatusCodeError(
|
||||||
return reject(
|
|
||||||
new StatusCodeError(
|
|
||||||
response.status,
|
response.status,
|
||||||
error,
|
error,
|
||||||
{ url, options },
|
{ url, options },
|
||||||
response,
|
response,
|
||||||
),
|
|
||||||
)
|
)
|
||||||
})
|
}
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const authHeaders = (accessToken) => {
|
const authHeaders = (accessToken) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue