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

co-try-catch

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

co-try-catch

go-lang-style error handling using co library

  • 0.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

co-try-catch

Provides a nicer way to handle errors avoiding ugly native try {} catch (e) {} indentation.

Example

const { tryCatch } = require('co-try-catch');

function *getData() {
  const response = yield tryCatch(makeAsyncRequest(options));
  if (response.failed()) {
      /* handle error */

     return /* and stop function execution */;
  }
 
  /* continue with the normal function execution */
  console.log(response.getResult());
}

...also, if you prefer...

const { err, result } = yield tryCatch(makeAsyncRequest(options));

Api

*tryCatch(gen: Function|Function*|Promise): TryCatchResult

TryCatchResult

isError(): Boolean

Returns if function has thrown an error

isSuccess(): Boolean

Returns if function didn't throw an error

failed(): Boolean

alias of isError()

succeeded(): Boolean

alias of isSuccess()

getError(): Mixed

Returns the thrown object

getResult(): Mixed

Returns result of the function execution

Supports nested calls

if a function call returns an instance of TryCatchResult it is passed up to the caller

const { tryCatch } = require('co-try-catch');

co(function*() {
  const exception = function *() { thrown new Error('test'); }
  const fn1 = function *() { return yield tryCatch(exception()); };
  const fn2 = function *() { return yield tryCatch(fn1)); };

  const result = tryCatch(f2());
  result.isError().should.equals(true);
  result.getError().message.should.equals('test');
});

Keywords

FAQs

Package last updated on 07 Oct 2016

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