New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

flatasync

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flatasync

`flatAsync` a simple wrapper to use Async/Await call without try/catch blocks.

latest
Source
npmnpm
Version
0.2.0
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

flatAsync

flatAsync a simple wrapper to use Async/Await calls without try/catch blocks. Flat style pattern to handle errors.

Install

npm i flatasync --save

Use

ES6/7

import flatAsync from 'flatasync'

NodeJs

const flatAsync = require('flatasync');

Example

import flatAsync from 'flatasync';

const [err, response] = await flatAsync(AsyncCall(...));

if (err) return console.error(err);
if (!response) return console.console('Not Found');

// use response here

Before

You must use try/catch in Javascript's await calls to handle errors from promise.

try {
  const user = await ajaxAPICall('/users/1');
  if(!user) return cb(null, 'User not found');
} catch(e) {
  return cb(e, 'Unexpected error occurred');
}
// another call based on user's data
try {
  const post = await ajaxAPICall(`/posts/${user.postId}`);
  if(!post) return cb(null, 'Post not found for user ${user.name}');
  else return return cb(null, post);
} catch(e) {
  return cb(e, 'Unexpected error occurred');
}

After

flatAsync wraps your await Promise or Method and returns errors/results in array [err, results].

import flatAsync from 'flatasync';
...

let user, post, err;

[err, user] = await flatAsync(AjaxCall('/user/1'));
if(err || !user) return cb(err, 'User not found');

[err, post] = await flatAsync(AjaxCall(`/posts/${user.postId}`));
if(err || !post) return cb(err, 'Post not found');

return cb(null, post);

Keywords

async

FAQs

Package last updated on 11 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts