0

I have the following function, by which I am attempting to set the value of the variable theString.

readStringFromFile is an asynchronous call that reads a text file and returns the first line of text within it.

This line of text is being retrieved and assigned to variable v. The alert box displays the line of text ok, as required.

However - 'return v' does not assign the value of v to theString (I guess because they are in different scopes?).

My question is: how can I adjust the code so that theString is assigned the value (v) that is retrieved from the asynchronous call?

There is a reason (not relevant here) why I have to wrap the asynchronous call inside the function that receives the id parameter, so I can't change that aspect.

I am new to jQuery so there may be basic errors here that need correction.

            theString = function(id)
            {
                self.readStringFromFile("<path>/myFile").then
                (
                    function(v)
                    {
                        alert(v);
                        return v;
                    }
                )
            }
QYH
  • 46
  • 3
  • 1
    This question is far too vague to be seen as anything other than a duplicate of [this question](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call). Perhaps if actually showed us how you're trying to use `theString` there may be something we could help you with. – JLRishe Mar 11 '15 at 19:02
  • Thanks, I will take a look and come back if needed. (Didn't find that thread earlier). – QYH Mar 11 '15 at 19:10
  • You assign a function to `theString`, not the read value (or a promise for it)? – Bergi Mar 11 '15 at 19:37
  • @JLRishe: I wonder whether we need a canonical duplicate for "*how to synchronously get the value from a promise?*" – Bergi Mar 11 '15 at 19:38
  • @Bergi [Fabrício Matté's question](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) and the popular close target above pretty much cover all the bases. I've just edited the question text of the former to include a promise example. [This recent question](http://stackoverflow.com/questions/28916710/what-does-mean-in-javascript-console-and-how-to-do-i-get-it) also talks about how to crack open a promise. – JLRishe Mar 11 '15 at 19:46
  • @JLRishe: Hm, imo that's too generic (and neither of the answers details the promise contracts). Heck, the answers even suggest "just use promises and all your problems will be solved" :-) It seems like many people expect that the `return` keyword does something magical, and asynchronous values are unwrapped at function boundaries. – Bergi Mar 11 '15 at 19:50
  • @Bergi Yeah, you're probably right about those questions not being quite the right fit. But I find that `return` is most effective when it's inside a `$.each()`, inside a callback: `var myValues = $.get('flights', { success: function (flights) { $.each(flights, function (flight) { return flight; }); } }); console.log(myValues);` – JLRishe Mar 11 '15 at 19:54

0 Answers0