Async await wrapper for easy error handling without try-catch
TypeScript JavaScript
Switch branches/tags
Nothing to show
Clone or download
scopsy Merge pull request #13 from rdsedmundo/add-commonjs-import-comment
Update adding instruct about NodeJS CommonJS import
Latest commit eef0198 Jun 28, 2018


NPM version Downloads

Async await wrapper for easy error handling


You need to use Node 7.6 (or later) or an ES7 transpiler in order to use async/await functionality. You can use babel or typescript for that.


npm i await-to-js --save


import to from 'await-to-js';
// If you use CommonJS (i.e NodeJS environment), it should be:
// const to = require('await-to-js').default;

async function asyncTaskWithCb(cb) {
     let err, user, savedUser, notification;

     [ err, user ] = await to(UserModel.findById(1));
     if(!user) return cb('No user found');

     [ err, savedTask ] = await to(TaskModel({userId:, name: 'Demo Task'}));
     if(err) return cb('Error occurred while saving task');

    if(user.notificationsEnabled) {
       [ err ] = await to(NotificationService.sendNotification(, 'Task Created'));
       if(err) return cb('Error while sending notification');

    if( !== {
       [ err, notification ] = await to(NotificationService.sendNotification(, 'Task was created for you'));
       if(err) return cb('Error while sending notification');

    cb(null, savedTask);

async function asyncFunctionWithThrow() {
  const [err, user] = await to(UserModel.findById(1));
  if (!user) throw new Error('User not found');

TypeScript usage

interface ServerResponse {
  test: number;

const p = Promise.resolve({test: 123});

const [err, data] = await to<ServerResponse>(p);


MIT © Dima Grossman && Tomer Barnea