Last active
August 16, 2022 01:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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