When a function reference is utilized, Coffeescript presents an unusual conversion of the try/catch/finally block

I recently wrote a helper function to handle errors, and I'm attempting to pass it as an argument to the catch function within a promise:

  fetchRecords().then (found) ->
    $scope.recprds = found;
  .catch( Session.handleError )
  .finally( -> $scope.querying = false )

When this code is translated into JavaScript, it looks like this:

  fetchRecords().then(function(found) {
    return $scope.records = found;
  })["catch"](Session.handleError)["finally"](function() {
    return $scope.querying = false;
  });

The issue arises because finally is not a property of my Session.handleError function, causing a JavaScript error.

Is there a different syntax that would be more appropriate in this scenario?

Experiment with it on coffeescript.org

Answer №1

In order to clean up your code, it's important to remove unnecessary semicolons and parentheses.

Reserved words like try, catch, and finally should be used as shown in the example on coffeescript.org:

try
  allHellBreaksLoose()
  catsAndDogsLivingTogether()
catch error
  print error
finally
  cleanUp()

To ensure that chaining works correctly in Coffeescript, consider renaming your helper functions:

fetchRecords().then (found) ->
    $scope.recprds = found
  .helpercatch Session.handleError 
  .helperfinally -> $scope.querying = false 

When parsed, your code should look like this:

fetchRecords().then(function(found) {
  return $scope.recprds = found;
}).helpercatch(Session.handleError).helperfinally(function() {
  return $scope.querying = false;
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transform jQuery code to its equivalent in vanilla JavaScript

While I am proficient in using jQuery, my knowledge of pure JavaScript is somewhat limited. Below is the jQuery code that I have been working with: $(document).ready(function() { $.get('http://jsonip.com/', function(r){ var ip_addre ...

What is the best way to create a TypeScript interface or type definition for my constant variable?

I'm facing challenges in defining an interface or type for my dataset, and encountering some errors. Here is the incorrect interfaces and code that I'm using: interface IVehicle { [key: number]: { model: string, year: number }; } interface IV ...

Tips for improving communication between sibling directives in AngularJS applications

All: Imagine I have two directives (dir1 and dir2), both with isolated scopes. After reading some posts, I understand that I need to use "require" to access the scope of the other directive, but one question continues to perplex me: If I use ng-repeat to ...

Ensuring Unique CSS for Diverse Devices: A User-Agent Driven Approach

I am facing multiple challenges and working towards finding solutions. I have developed a webpage and now I want to redirect the link to the CSS file based on the user agent. `<script type="text/css" src="style.css"> </script>` However, inst ...

ng-click does not run when constructed as a string in the link function

Currently, I am utilizing Timeline JS to create a chronological timeline. I have integrated elements into the timeline and would like them to be clickable. Within my directive's link function, I am constructing the content of the clickable element in ...

Use a dropdown menu to update the selected value

Issue with displaying drop down values in the second list, despite trying various solutions. When a user selects a country, the corresponding state should be populated from the database into the second drop-down. Any assistance would be greatly appreciated ...

Adjust the iframe height when using slidetoggle

I currently have a menu positioned in the center of the screen, set to "show" by default. Beneath this menu, there is an iframe filling up the remaining space: <script type="text/javascript> $(document).ready(function() { var main_height = ($(d ...

Tips for inserting a button under a div when clicked

I am working on a layout where I have several div cards aligned side by side using the display: inline-block property. What I want to achieve is that when I click on a card, a button is added below the respective div. To accomplish this, I tried using jqu ...

The $scope.$apply function is causing an exception because a $scope.apply process is already underway

I am facing an issue while trying to update my view based on the changes made to my $scope variables. During the digest cycle, after modifying the variables, I call $scope.$apply(). However, I receive an exception saying that $scope.$apply is already in ...

Looping through all the asp:Hyperlink controls using Javascript and then concealing them on a webpage

In my website's master page, there is a DIV containing asp:Hyperlink controls that serves as the navigation menu. On a specific page titled 'Certain Page', I would like to use JavaScript to loop through all asp:hyperlink controls and hide t ...

When invoking a native prototype method, consider extending or inheriting object prototypes for added functionality

Recently, I came across a discussion on Inheritance and the prototype chain where it mentioned: Avoiding bad practice: Extension of native prototypes One common mis-feature is extending Object.prototype or other built-in prototypes. This practice, known ...

What is the method to check for the absence of an anchor element in jest and react-testing-library?

In my React component, a link is rendered based on a specific rule. The markup looks like this when there is a link within the h4 element: <h4><a href="">foo</a></h4> <p>blah blah <a href="">bar</ ...

The functionality of HTML5 canvas image objects is not functioning as expected

I have been working on a function to retrieve an image object using HTML5 canvas, but I keep encountering an error alert (onerror event) function FetchImage() { var img = new Image(); img.src = "http://localhost/assets/images/loadedsprite.png"; ...

Encountering a SystemJS error while trying to import modules in AngularJS can be frustrating. The error message stating that only modules loaded by SystemJS.import are

Currently, I am in the process of upgrading an AngularJS application to Angular. I am utilizing the standard ngUpgrade module for this task. After successfully converting a service to Angular, I now need to downgrade it in order to incorporate it into an e ...

Adjust website content depending on user's authentication status

My goal is to display a logout button when the user is logged in and a login button if they are not. I am using JSON tokens to determine if a user is logged in or not, by checking if the token is null. However, this approach does not seem to be working. Ca ...

How can you add an object to an array in JavaScript without knowing the index?

I am working with an array var prices = ['100','200','300','500']; In my code, I am trying to insert a price if certain conditions are met. var index = prices.indexOf(400); if(index){ prices.splice(1, ...

The AngularJS DOM seems to be updating, but the screen is not reflecting any changes. Uncertain about the next steps to take

I am experiencing a perplexing issue where, despite ng-click updating the expected scope variables and the changes being visible in the DOM via the Chrome debugger, the altered DOM elements are not redrawn in the browser. This anomaly occurs consistently o ...

What could be the reason for the malfunctioning dropdown menu in my navigation bar upon clicking it?

After spending hours practicing creating a navbar using Bootstrap 3.3.7, I've hit a roadblock - the dropdown is not working when clicked on. It's frustrating because I have double-checked all my scripts and ensured that I have the latest version ...

Having issues with the Vuetify component, specifically the v-date-picker not functioning properly

Hey there! I'm still getting the hang of vuetify and have been experimenting with creating functions using simple components. One such component I've been using is v-date-picker. However, I've encountered an issue where the calendar doesn&a ...

Reveal the table only once a search has been completed

Currently, I am in the process of developing a small project and my aim is to have the table with the results appear only upon clicking the button or initiating a search. If you are interested, you can view it at this link: . My objective is to keep the t ...