New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

catching

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

catching

Catch promise reject, return as promise resolve.

  • 1.0.3
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by120%
Maintainers
1
Weekly downloads
 
Created
Source

catching

Catch promise reject, return as promise resolve.

Often used in async function, together with await.

Install

npm install catching

Use

import catching from 'catching'

async doOperation() {
  const ret = await catching(yourPromiseFn())
  // Here you need differ resolve and reject by: code or instanceof Error, etc.
  const isOK = ...

  if (!isOK) {
    throw ret
  }
  else {
    // ... next step
  }
}

Why

Normally when we use async/await, we would use try-catch for promise reject scenario.

But the code written by try-catch is not concise, while it indeed differentiates abnormal case from normal.

Code looks like below, addUserDAO reject promise while meeting error.

async addUserService() {
  try {
    const ret = await addUserDAO()
  }
  catch (err) {
    throw err
  }
}

function addUserDAO () {
  return new Promise((res, rej) => {
    // const isOK = ...
    if (OK) {
      res({ data }) 
    }
    else {
      rej({ data, code })  
    }
  })
}

How

import catching from 'catching'

async addUserService() {
  const ret = await catching(addUserDAO())
  if (!ret.code) {
    throw ret
  }
  else {
    // ... next step
  }
}

  • How to write async await without try-catch blocks in Javascript
  • try/catch blocks with async/await

Keywords

FAQs

Package last updated on 02 Aug 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc