It also helps to familiarize with async/await constructs.
You concluded that 'Promises are difficult'. Please try to understand the main reason they exist (I am not telling you that now, sorry) - you will then see they are not so difficult at all.
We can see that the then is just returning the value from the original promise and the catch is also just rejecting with the already rejected value. That means we're just returning the original promise, which can be simplified to:
Thanks for reading. The verbosity was intentional. The point was to show how to use Promises while writing functions as a beginner. Using arrow functions surely uses less lines of code and I use that in my actual projects. However explaining Promises to my juniors using arrow function was always difficult. I am familiar with async/await constructs. I still prefer Promises and use it as much as possible. But my colleagues who came from Java background they preferred async/await more 😀
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi,
on your template, you seem to be enjoying the verbosity. If not, I recommend:
and since you are using arrow functions, do you always use parentheses? This:
becomes..
Wait, what?!
This:
.. easily becomes this:
Back to your promise template. Here's a much lighter version. You don't need to reject, you throw:
Then on this monstrosity:
Consider changing it into:
It also helps to familiarize with
async/await
constructs.You concluded that 'Promises are difficult'. Please try to understand the main reason they exist (I am not telling you that now, sorry) - you will then see they are not so difficult at all.
Maybe the point of the article was to show how simple it is to wrap everything in a promise, but starting with the original:
And combining with your suggestion (and doing the same with reject):
We can see that the then is just returning the value from the original promise and the catch is also just rejecting with the already rejected value. That means we're just returning the original promise, which can be simplified to:
Which is just the
fn_to_get_roll_number_from_db
function.Thanks for reading. The verbosity was intentional. The point was to show how to use Promises while writing functions as a beginner. Using arrow functions surely uses less lines of code and I use that in my actual projects. However explaining Promises to my juniors using arrow function was always difficult. I am familiar with
async/await
constructs. I still prefer Promises and use it as much as possible. But my colleagues who came from Java background they preferredasync/await
more 😀