Skip to content

Instantly share code, notes, and snippets.

@keidarcy
Last active August 16, 2022 01:10
Show Gist options
  • Save keidarcy/55ddd9f5cb7fa305072395a12f145294 to your computer and use it in GitHub Desktop.
Save keidarcy/55ddd9f5cb7fa305072395a12f145294 to your computer and use it in GitHub Desktop.
How to use Promise.allSettled and fetch api with GET and POST together
const p1 = new Promise((resolve, reject) => setTimeout(resolve(1), 200))
const p2 = new Promise((resolve, reject) => setTimeout(reject('error'), 300))
const p3 = new Promise((resolve, reject) => setTimeout(resolve(3), 4000))
Promise.all([p1, p2, p3]).then((res) => console.log(res)).catch(e => console.log(e))
Promise.allSettled([p1, p2, p3]).then((res) => console.log(res)).catch(e => console.log(e))
btn.disabled = true
const id = btn.dataset.variantId
const payload = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ id })
}
Promise.allSettled([
fetch('/cart/add', payload),
fetch(
`${ONLINE_STORE_ENDPOINT_CLICK}?shop=${Mirissa.Store.DomainName}&cart=true`
)
])
.then(async ([addCartRaw, addClickCountRaw]) => {
const addCartResponse: Response = addCartRaw.value
const addClickCountResponse: Response = addClickCountRaw.value
return [await addCartResponse.json(), await addClickCountResponse.json()]
})
.then((res) => console.log(res))
.catch((err) => console.log(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment