Created
January 21, 2024 08:52
-
-
Save SuperCipher/4ea57c7f521abdafae1a7a2eb34c89e1 to your computer and use it in GitHub Desktop.
promise all constructor
This file contains hidden or 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
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