Skip to content

Instantly share code, notes, and snippets.

@SuperCipher
Created January 21, 2024 08:52
Show Gist options
  • Save SuperCipher/4ea57c7f521abdafae1a7a2eb34c89e1 to your computer and use it in GitHub Desktop.
Save SuperCipher/4ea57c7f521abdafae1a7a2eb34c89e1 to your computer and use it in GitHub Desktop.
promise all constructor
function createPromises(onFunctions) {
return onFunctions.map(onFunction => {
return new Promise((resolve, reject) => {
try {
onFunction(resolve)
} catch (e) {
reject(new Error(`Error in function ${onFunction.name}: ${e}`))
}
})
})
}
// Usage:
const multiPromises = createPromises([onFCP, onLCP])
Promise.all(multiPromises)
// , fcp, fid, lcp, inp
.then(arr => {
console.log('🚀 ~ .then ~ arr:', arr)
})
.catch(error => {
console.log('🚀 ~ Promise.all ~ error:', error)
// Handle any error that occurred in the previous steps
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment