I like to deal with all the error cases first, and then dealing with the heart of the function. Also I like to create small functions that deal with a single responsibility, this is easier to work with IMO.
I have sort of a kink with promises now and I like to put them everywhere. I like the simplicity of chaining functions.
"use strict";conststring=something=>JSON.stringify(something);constdivide=(numerator,denominator)=>newPromise((resolve,reject)=>{if(typeofnumerator!=="number"){returnreject(newTypeError(`numerator should be a number in divide(${string(numerator)}, ${string(denominator)})`));}if(typeofdenominator!=="number"){returnreject(newTypeError(`denominator should be a number in divide(${string(numerator)}, ${string(denominator)})`));}if(0===denominator){returnreject(newError(`denominator should not be zero in divide(${string(numerator)}, ${string(denominator)})`));}returnresolve(numerator/denominator);});divide(1,2).then(result=>divide(result,2)).then(result=>divide(result,2)).then(result=>console.log(`After dividing by two three times: ${result}.`))// After dividing by two three times: 0.125..catch(({message})=>console.error(`error while dividing: ${message}.`));divide(1,0).then(result=>divide(result,2)).then(result=>divide(result,2)).then(result=>console.log(`After dividing by two three times: ${result}.`)).catch(({message})=>console.error(`error while dividing: ${message}.`));// error while dividing: denominator should not be zero in divide(1, 0).
I am not sure turning inherently synchronous code into async is a good idea. Promises are OK (not great - observables are better in many cases) for async operations but I would definitely avoid introducing the complexity for inherently sync operations.
By using this pattern, you are forcing all your code to be async.
THANK YOU! YES, I was soooo tempted to touch on Promises but then majority of folks who read my blogs (maybe less so now?) are beginners so I'm planning to write a separate thing on promises in general. However, I'll link your comment in the article because I love your example! Thank you.
If you use async your code becomes a bit more natural:
asyncfunctiondivide(numerator,denominator){if(typeofnumerator!=="number"){thrownewTypeError(`numerator should be a number in divide(${numerator}, ${denominator})`);}if(typeofdenominator!=="number"){thrownewTypeError(`denominator should be a number in divide(${numerator}, ${denominator})`);}if(0===denominator){thrownewError(`denominator should not be zero in divide(${numerator}, ${denominator})`);}returnnumerator/denominator;});
This however indicates that it would be probably be smart to remove the async and move that to a separate function. That would allow you to use the divide function in more scenario's.
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 there, great article, thank you!
I like to deal with all the error cases first, and then dealing with the heart of the function. Also I like to create small functions that deal with a single responsibility, this is easier to work with IMO.
I have sort of a kink with promises now and I like to put them everywhere. I like the simplicity of chaining functions.
This is an awesome demonstration! I need to start learning and living this pattern for larger functionality.
I am not sure turning inherently synchronous code into async is a good idea. Promises are OK (not great - observables are better in many cases) for async operations but I would definitely avoid introducing the complexity for inherently sync operations.
By using this pattern, you are forcing all your code to be async.
THANK YOU! YES, I was soooo tempted to touch on Promises but then majority of folks who read my blogs (maybe less so now?) are beginners so I'm planning to write a separate thing on promises in general. However, I'll link your comment in the article because I love your example! Thank you.
If you use
async
your code becomes a bit more natural:This however indicates that it would be probably be smart to remove the
async
and move that to a separate function. That would allow you to use thedivide
function in more scenario's.