question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

tryCatch should work with nAry arguments

See original GitHub issue

Currently, tryCatch always returns unary functions even if the input passed to it is nAry.

We can change this behaviour with the proposed implementation, if this doesn’t cause any breaking problems.

const tryCatch =  function tryCatch(fn) {
  if(!isFunction(fn)) {
    throw new TypeError('tryCatch: Function required for first argument')
  }

  const safe = function() {
    try { return Ok(fn.apply(this, arguments)) }
    catch(e) { return Err(e) }
  }

  Object.defineProperty(safe, "length", { value: fn.length })
  
  return safe
}


const add = tryCatch((a, b) => {
  return a + b
})

add(1, 2) => 3
curry(add)(1)(2) => 3

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
karthikiyengarcommented, Oct 2, 2018

I don’t see problems with the defineProperty itself, but the spread operator is going to destroy context. Edited to use the old-school way.

0reactions
evilsoftcommented, Oct 28, 2018

Shipped with 0.11.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

When to use Try/Catch? - webMethods
I'm sometimes asked to assess the quality of work performed by other consulting firms. When I find massive numbers of Flow services with...
Read more >
How do you make tryCatch actually catch the error
You can pass a function to the error argument of tryCatch to specify what should happen when there is an error. In this...
Read more >
kdb+/q - Try Catch - Java Code Geeks - 2022
g – a function to execute if f fails. This function is called with two arguments, the error string x and the backtrace...
Read more >
Using tryCatch Function to Handle Errors & Warnings in R (3 ...
Within the tryCatch function, we usually should specify four arguments: expr: This specifies the expression we want to evaluate. error: The message we...
Read more >
How to Apply Try Catch Block in PHP - Linux Hint
The script will take two numeric values from the URL query parameters and the try-catch block will call a function that will throw...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found