JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 769166
  • Score
    100M100P100Q177063F
  • License MIT

Async/await wrapper for easy error handling in js

Package Exports

  • await-to-js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (await-to-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

await-to-js

NPM version Downloads

Async await wrapper for easy error handling

Pre-requisites

You need ES7 transpiler in order to use async/await functionality. You can use babel or typescript for that.

Install

npm i await-to-js --save

Usage

import to from 'await-to-js';

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

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

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

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

    cb(null, savedTask);
}

License

MIT © Dima Grossman && Tomer Barnea