Return a Default Value with Promises Using catch
catch to set a default value instead of a
// Instead of wrapping this block in a try/catch...
const result = await new Promise(
(resolve, reject) => reject("Because Stone Cold said so")
// ...use catch to set the default value (100)
.catch(() => 100);
As a trick I really like what's this brings; as far as readability and maintenance, if you don't know the trick, you might get confused. What do you think?
You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...
I love almost every part of being a tech blogger: learning, preaching, bantering, researching. The one part about blogging that I absolutely loathe: dealing with SPAM comments. For the past two years, my blog has registered 8,000+ SPAM comments per day. PER DAY. Bloating my database...