Don’t abuse language features

Recently we had a situation in a ES6 project. We were using some of new features ES6 in a wrong way. I’m writing this blog post to explain what was the issue and what is the solution to avoid these kind of problems it the future.

ES6 Promise

We designed a Message box class which accepts a title, text and buttons. Then, you can call open to display the message box. User can decide to click on Yes or No button (assume there is a Accept / Reject button on the message box).

That open function returns a Promise which fulfills the Promise if user clicked on Yes and rejects it if you click on No button. Therefore you can decide what to do with the Promise.

This is sample of that API:

let msg = Message({ 
	text: "Hello world", 
	title: "Are you sure?",
}).open(); //msg is a Promise now 


Now, to test and see if user clicked on Accept or Reject button, you can use then() or catch() of Promise object. But we didn’t use these functions. Instead, we use try...catch with async / await keywords.

Incorrect usage of Promise

Another way to handle Promise is to use async and await keywords, like so:


(async function () {
	try {
  	let res = await ReturnPromise();
    console.log("gotcha", res);
  } catch (e) {


It would reach the catch block if user clicked on Reject and the try block if Accept (actually, it would execute anything after await keyword, otherwise, it throws an exception and hits the catch block).

Well, above usage is correct, too, but there are many problem with that approach:

1) Suppressing errors

If you add anything to that try block, you would need to handle the errors on the catch block. If you don’t handle them in try catch block, it would suppress the errors and wouldn’t be able to understand what is going on in your application in case of any errors.

2) Catch block

You cannot remove the catch block. You should either leave it empty or add something between the catch block. Imagine your application with a milion of empty catch blocks:

(async function () {
	try {
  	let res = await ReturnPromise();
    console.log("gotcha", res);
  } catch (e) { } // <-- this is the empty block


Also, sometimes you just need to handle the Accept case and there is no need to handle the catch or Reject case. Now, you can’t do anything but adding an empty catch block.

3) You are not handling an error!

You should use try catch when you want to handle an error and do something instead of letting the application to crash and exit the main. Is that case, we were not handling an error. Clicking on a Reject button is not an error.


Write maintainable code

Remember, you are writing codes and it should be readable and understandable for other contributors and if you use some sort of weird syntax in your program, it doesn’t prove anything about the level of your coding skills or how talented you are. A good code is boring to read.

Programming languages war

So frequently I hear a tedious conversation between my colleagues, “PHP is better than foo”, “.NET is better than boo” etc. etc. Each time I hear this sort of dialog I try to ask the reasons of this comparison but till now, no one has given a proper answer since somehow it’s impossible.

Up till now, I’ve coded with JavaScript, PHP, C# and little bit Python. I’m still a newbie in programming industry, but at least I had a lot of mutual projects with some experts. The result of being a part of those projects, taught me how to tackle a problem, how to choose an adequate tools or language and prepare the environment to solve it. Almost in all cases, indicating the programming language wasn’t the bottleneck. However, we did that with considering the problem’s parameters. Design a good architecture and implementing it correctly was the main goal in our projects.

I’d like to point it that obviously, the programming environment is not the only parameter to have a robust application. The most major parameter to have a good result, is the knowledge of programmers, not the facilities of programming languages. I don’t use more than 50% of programming languages features in the progress of development, and I bet no one else will either.

As time passed by, old-fashioned programming languages get retired and new technologies come into the battlefield. Consequently, it’s not a value to know a programming language well, it would be better if you know the concepts.

DISCLAIMER: Above writing is my thoughts and obviously you don’t have to be agreed with necessarily.

Source: New feed