0

I am new to coffeescript and I have a coffee script code as

getProviderListDisplayValues:(domainId) ->      
    displayValues = []
    $.ajax 
        contentType: 'application/json',
        url: "/Services/ListProviders?domainid=#{domainId}",
        success: (data) ->          
            for oneResponse in data
                displayValues.push oneResponse.name     
    displayValues

which is compiled to

CounselorHome.prototype.getProviderListValues = function(domainId) {
   var values;
   values = [];
   $.ajax({
     contentType: 'application/json',
     url: "/Services/ListProviders?domainid=" + domainId,
     success: function(data) {
       var oneResponse, _i, _len, _results;
       _results = [];
       for (_i = 0, _len = data.length; _i < _len; _i++) {
         oneResponse = data[_i];
         _results.push(values.push(oneResponse.id));
       }
       return _results;
     }
   });
   return values;
};

I just want to push values to values[] & displayValues[] but why is the _results[] array created? Does it hampers the browser efficiency? Is there any way removing such unnessary code? May be, by editing my coffee script.

EDIT : WORKING CONDITION

But when I put an alerting code as

 $.ajax 
        contentType: 'application/json',
        url: "/Services/ListProviders?domainid=#{domainId}",
        success: (data) ->          
            for oneResponse in data
                displayValues.push oneResponse.name     
 alert displayValues
 displayValues

This code works and I can retrieve the required data.

2
  • ajax is asynchronous so I think you're trying to do something that won't work as you expect. It looks like coffee notices this, and so never gives you _results in a referencable way Commented Aug 28, 2014 at 11:59
  • Yes, while reurning values[] it returns undefined for Google Chrome and null for FireFox. But I have written code to push all data to values[]. Hence, the problem. Commented Aug 28, 2014 at 12:06

1 Answer 1

2

Apart from that your code wouldn't work anyway, the _results are generated because of coffeescripts implicit function return values - and loops are only expressions as well that generate arrays. The docs state:

Sometimes functions end with loops that are intended to run only for their side-effects. Be careful that you're not accidentally returning the results of the comprehension in these cases, by adding a meaningful return value — like true — or null, to the bottom of your function.

So the javascript that you expected can be created by writing

…
    success: (data) ->
        for oneResponse in data
            displayValues.push oneResponse.name
        return
…

("Trailing return and return undefined are now optimized away." - since version 1.0.1);
See also Is there any way to not return something using CoffeeScript?

However, what you actually want is this:

getProviderListDisplayValues:(domainId) ->
    $.ajax
        contentType: 'application/json',
        url: "/Services/ListProviders?domainid=#{domainId}"
    .then (data) ->          
        for oneResponse in data
            oneResponse.name

…

getProviderListDisplayValues(…).then (displayValues) ->
    …
Sign up to request clarification or add additional context in comments.

4 Comments

I was a bit less strong with the won't work as expected rather than wouldn't work anyway, because OP's code doesn't actually destroy the reference to displayValues (until it's converted), meaning that after success, it should be populated. It's just there is nothing letting the rest of the script know the success function has completed ... and coffee's conversion.
@PaulS.: Yeah, and that makes the displayValues array unusable - I was so bold because I expected the OP to use the array just after it was returned. The mistake is just too common :-/
If so, how to populate values[], because I need to return an array?
You can't return an array which you are getting asynchronously from your function. That's just impossible (and using alert like in your edit only does some weird timing things that you don't want). The function in my answer does return a promise for the array. See the question I linked for further details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.