Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

elegant-expression

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elegant-expression

Library provides try/catch/finally as expression

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

Elegant Expression

Actions Status Coverage

Library provides try/catch/finally as expression.

Install

npm i elegant-expression -S

Try/Catch/Finally

JavaScript and TypeScript (accordingly) provide the try/catch/finally only as statement.

When using a block of try/catch/finally creating different scopes and you will need to declare ahead the variables that are used within blocks. Additional, you should manual set types for this variables (you can't using smart detection types of variables) and make casting of error type in block of catch.

All this leads to the writing of boilerplate code and makes it difficult to read:


// Before 😮‍💨
let status: 'ok' | 'fail';
try {
  await fn();
  status = 'ok';
// error has type unknown
} catch (err: unknown) {
  const error = err as Error;
  status = 'fail';
}

// After 🙆‍♂️
import exp from 'elegant-expression';

// We get result of executing immediately in one place
const status = await exp
  .try(async () => {
    await fn();
    return 'ok';
  })
  // error already has type Error
  .catch((err: Error) => {
    console.error(err);
    return 'fail';
  });

try/catch/finally:

const result = await exp
  .try(async () => {
    await fn();
    return 'ok';
  }).catch((err) =>
    'fail'
  ).finally(() =>
    console.log('finally');
  );

try/finally:

const result = await exp
  .try(async () => {
    await fn();
    return 'ok';
  }).finally(() =>
    console.log('finally');
  );

Keywords

FAQs

Package last updated on 13 Jun 2024

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