await-to-js

WebJar for await-to-js

License

License

MIT
Categories

Categories

JavaScript Languages
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

await-to-js
Last Version

Last Version

2.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

await-to-js
WebJar for await-to-js
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/scopsy/await-to-js

Download await-to-js

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/await-to-js/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>await-to-js</artifactId>
    <version>2.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/await-to-js/
implementation 'org.webjars.npm:await-to-js:2.1.1'
// https://jarcasting.com/artifacts/org.webjars.npm/await-to-js/
implementation ("org.webjars.npm:await-to-js:2.1.1")
'org.webjars.npm:await-to-js:jar:2.1.1'
<dependency org="org.webjars.npm" name="await-to-js" rev="2.1.1">
  <artifact name="await-to-js" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='await-to-js', version='2.1.1')
)
libraryDependencies += "org.webjars.npm" % "await-to-js" % "2.1.1"
[org.webjars.npm/await-to-js "2.1.1"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

await-to-js

NPM version Downloads

Async await wrapper for easy error handling

Pre-requisites

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.

Install

npm i await-to-js --save

Usage

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, savedTask, notification;

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

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

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

    if(savedTask.assignedUser.id !== user.id) {
       [ 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);
}

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);
console.log(data.test);

License

MIT © Dima Grossman && Tomer Barnea

Versions

Version
2.1.1