Skip to content

Instantly share code, notes, and snippets.

@feliperohdee
Created December 24, 2022 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feliperohdee/709073beb3644f2fff89553c2b365696 to your computer and use it in GitHub Desktop.
Save feliperohdee/709073beb3644f2fff89553c2b365696 to your computer and use it in GitHub Desktop.
promise-all-4.js
const putData = async (data, ms) => {
return new Promise(resolve => {
setTimeout(() => {
resolve(data);
}, ms);
});
};
const clearAll = async () => {
// logic to clear all data
};
const handleSettledPromiseValue = (results = []) => {
return results.map(result => {
if (typeof result !== 'object') {
return result;
}
if (result.status !== 'fulfilled') {
throw result.reason;
}
return result.value;
});
};
try {
const [
result1,
result2,
result3
] = handleSettledPromiseValue(await Promise.allSettled([
putData({
email: 'user@email.com'
}, 1000),
putData({
apiKey: 'api-token'
}, 2000),
putData({
settings: {
accountName: 'John Doe',
accountType: 'Premium'
}
}, 3000)
]));
} catch(err) {
// an error happened and we need to clear all data
await clearAll();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment