Three useful patterns to make better use of async/await code in Javascript

Photo by Wim van 't Einde on Unsplash

1. Use function instead of

Don’t: Pass directly as a second parameter of . does not catch error thrown by :

getPromise().then(process, handleError)

Do: Pass in . works for errors thrown by either or :


2. Use finally to execute some code after a promise fulfils or rejects

Don’t: Invoke the same code in and

.then(response => {
.catch(e => {

Do: Make use of finally

.then(response => {
.catch(e => {
.finally(() => {

3. Wrapping ephemeral resources inside an async function

This pattern builds on the previous one to make…

After more than a decade of entrusting Evernote with my notes, I finally left the service.

Photo by Keyur Nandaniya on Unsplash

I started taking notes seriously with Evernote more than 10 years ago. Note taking is a wheel that has been re-invented over and over again a few times. Allowing people to write notes is the easy part. After all, we have had word processing software for decades, physical notebooks for centuries and clay tables for millennia. The difficult part of the job is how to store them safely and retrieve them quickly. …

A common pitfall with the constructor of Javascript’s Date object. It uses a base 0 for monthIndex parameter, but base 1 for others.

Photo by Charles Deluvio on Unsplash

So I needed to perform some date operations in Javascript, let’s take for this example. It was a sunny ☀️ day here in the UK. And it was International Women’s Day, which was a big deal in many places. Anyway’s let’s get back to Javascript.

I needed to convert the date to a object in Javascript so I could do things like calculating the day of the week, or how many days have passed since then.

Javascript has four constructors for the object:

new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex [, day [, hours [, minutes…

The default behaviour of Array.prototype.sort() in Javascript may not be what you expect if no compare function is provided.

Photo by Faris Mohammed on Unsplash

I needed to sort an array of integers in Javascript in ascending order recently and reached for .

const array = [0, 2, 1, -2, -1]
//expected result: [-2, -1, 0, 1, 2]

I have used this function many times over and have never encountered any problem. I am aware that you can pass in a compare function as an optional parameter to customise the sorting behaviour, i.e. . But for plain old sorting integers ascendingly, the assumption was it was an overkill.

How wrong I was 🤦‍♂! The value of after being sorted was:

[ -1, -2…

Array is one of the most important and elegant features in Javascript. Developers, especially those coming from statically typed languages like Java or C# tend to overlook the built-in methods of Javascript’s array.

Photo by Junsheng Chen on Unsplash

There are two main ways to create an array in Javascript.

Using class:

let a = new Array
let b = new Array(1, 2, 3)

and using syntax:

let c = []
let d = ['a', 'b', 'c']

Javascript is a dynamically typed language, so each item in the array can be of a different type. Think of it as an in Java, or in C#.

let a = [1, '🐓', '🥚', () => {}]
console.log(typeof a[0]) //number
console.log(typeof a[1]) //string
console.log(typeof a[2]) //string
console.log(typeof a[3]) //function

Four ways to loop through an array

There are more ways to traverse an array than…

An explanation about how AWS resources can be tagged using the Serverless framework. Code sample is available in GitHub.

Resources tagging is an important mechanism in the AWS ecosystem. They can be used for DevOps automation and cost allocation, among other things. A tag is a pair of key and value that can assigned to most resources. A resource can have a maximum of 50 tags. AWS automatically assigns some tags with prefix.

Whilst you work with the Serverless framework to develop a solution for AWS, the framework provisions the resources on your behalf. Some of those resources are provisioned explicitly, i.e. as a result of CloudFormation script inside the section of . Other resources are provisioned…

In AWS CloudFormation, all resource types have a DeletionPolicy attribute with possible values of , and . Setting it to would save the resource from being deleted by mistake.

If no deletion policy is specified for a resource, the default value is . It means the resource will be removed as part of the CloudFormation stack removal.

For resources with a deletion policy, when the stack is deleted, AWS CloudFormation leaves the resource without deleting it.

However, it does NOT stop the resource from being deleted directly:

  • Programmatically, i.e. using AWS CLI tool.
  • Manually, i.e. using the AWS web console.

With the DeletionPolicy attribute you can preserve or (in some cases) backup a resource when its stack is deleted. Resources that support deletion policy include:

Solutions to install any versions of Node.js on a RedHat-based Linux, including RedHat, Fedora, Oracle Linux and Centos.

Installing latest version of a specific Node.js stream

The latest Long-Term-Support (LTS) version of Node.js can be installed to RedHat-based Linux using the package manager:

dnf module -y install nodejs:<stream>

where corresponds a major LTS version of Node.js, e.g. 10, 12 or 14. You can list the available streams using:

dnf module list nodejs

This method will only allow you to install the latest LTS versions, e.g. at the time of writing

  • v8.17.0 for Node.js 8
  • v10.23.1 for Node.js 10
  • v12.20.1 for Node.js 12
  • v14.15.4 for Node.js 14

If you want to install a specific version of Node.js, you need to be more involved.

Installing any Node.js version

This is…

Run this script on the terminal:

node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

This script will generate a random 64 bit ASCII string, e.g. that can be used for encrypting JWT tokens.

Let’s explain the magic:

  1. tells Node.js to evaluate a script, in this case, a Javascript string
  2. is the cryptographic module forming part of Node.js core. It is already installed as part of Node.js, no extra npm package is involved.
  3. is a function that generates cryptographically strong pseudo-random data. It will return a Buffer object.
  4. is a method of the Buffer class that decodes the object to a…

Let’s talk quickly about what it is. According to wikipedia:

lint, or a linter, is a static code analysis tool used to flag programming errors, bugs, stylistic errors, and suspicious constructs.

Its biggest benefit is to remove the opinionated personal preferences from codebases with multiple contributors. A function written by a developer should looks similar to another one written by someone else. It ensures homogeneity which in turn make the repository easier to maintain.

Code linting is important for any projects that are more than the trivial hello world example.

The the Node.js ecosystem, ESLint is by far and away…

Quynh Nguyen

Software developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store