Last active
September 2, 2022 23:03
-
-
Save vielhuber/43f45c742be3a8681376d8fd64136fe5 to your computer and use it in GitHub Desktop.
#js promise constructor antipattern
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
// https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it | |
// bad | |
function_name() | |
{ | |
return new Promise((resolve,reject) => { | |
// do some async task that returns a promise | |
someAsyncTask().then((result) => | |
{ | |
// modify the result if needed | |
result++; | |
resolve(result); | |
}).catch((error) => | |
{ | |
console.log(error); | |
reject(); | |
}); | |
}); | |
} | |
// good | |
function_name() | |
{ | |
return someAsyncTask().then((result) => | |
{ | |
result++; | |
return result; | |
}).catch((error) => | |
{ | |
console.log(error); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment