Native JavaScript Promise Example with Prompt

JavaScript on modern browsers has a native support for promises. And the greatest thing is that the syntax is very easy.

Basically you can use promises for anything but they are at their best with asynchronous actions. For example ajax, user input, timeout. In this example we are using native JavaScript dialog: prompt.

Lets first check out the basic syntax:

We can also chain the whole thing like this:

Or cut it to small functions:

Here I have made a little script that generates Alphabet squares. First it asks for user input and then creates rows and finally prints them. If user’s input is invalid it will get caught in error handling.

The final result gives you alphabet squares depending on the size of the number. For example: