Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active September 2, 2022 23:03
Show Gist options
  • Save vielhuber/43f45c742be3a8681376d8fd64136fe5 to your computer and use it in GitHub Desktop.
Save vielhuber/43f45c742be3a8681376d8fd64136fe5 to your computer and use it in GitHub Desktop.
#js promise constructor antipattern
// 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