It’s been a long time since my previous post. So many things have been changed in my life. I moved to Sweden and was quite busy last months because of this. But it’s calming down now and I found some time for my blog.

As I promised previously I’m writing this blog post about errors handling with async/awaits. This one will be just a short note to show an idea of how Promises and async/awaits can work together. Actually, errors handling is a huge topic in software development, but let’s remember only the most popular ways to handle errors in JavaScript:

  • Passing error object in callbacks
  • try...catch statement in general cases and for async/awaits
  • .catch() when using Promises

As we’re working with async/awaits and Promises, let’s took only the last two.

Sometimes I hear an opinion that if you decided to move from Promises to async/awaits to simplify your code and there was a Promise.catch you have to use try...catch statement to convert this. But that’s not actually true, you can mix these approaches to leave your code free from ugly try...catch statements.

I think I have a good example to demonstrate this. Let’s look onto a new Node.js Promises file system API. There is a function access to check if a file is accessible by the current process. It returns a Promise which resolves if it is possible to access a target file and rejects otherwise. If we will try to use that with async/await it’ll look like this:

async function isAccessible(path) {
    let result;

    try {
        await fs.access(path);
        result = true;
    } catch (err) {
        result = false;
    }

    return result;
}

That one doesn’t look nice. But what if we’ll try to add a Promise error handling approach here?

async function isAccessible(path) {
    const result = await fs.access(path)
        .then(() => true)
        .catch(() => false);

    return result;
}

So how that could be possible? We know that returned Promise from access resolves and rejects depending from the check result, so I think it’s a nice idea to convert this behavior to the values we need: true or false.

Moreover, there’s no need to make this function async and add await to get a result. Actually, it’s possible to simplify it further.

function isAccessible(path) {
    return fs.access(path)
        .then(() => true)
        .catch(() => false);
}

// Usage

if (await isAccessible('/etc/passwd')) {
    console.log('The passwd file can be read by current process.');
} else {
    console.log('The passwd file can\'t be read by current process.');
}

Here is a really handy function isAccessible which returns Promise and it always resolves true or false and never rejects.

So what we got here? We should always keep in mind that Promises and async/awaits are tightly connected in JavaScript and use this knowledge to keep the code as clean as possible.

That’s it, thanks for reading this. Hope this note was helpful.

See also

  1. Promise.catch MDN article with nice examples and comprehensive description
  2. Node’s promises FS API documentation