in Education by
It's probably obvious to you, but I can't figure it out. I need to make function that returns it's inner-function's value. In other words, I have function get_users() that must return JSON object. That JSON object is got by $.post (built-in jQuery). function get_users() { return $.post( url_base + 'travel/trip/get_users/' + trip_id, function(response) { return response; }, 'json' ); } (above is what I tried to do, but it returned undefined - what a surprise) Because of variable scope, I cannot just make variable in inner-function because it won't be visible in main function. I don't want to use global variables neither. Looking for better solution! Thanks in any advice! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Why are you fighting against the asynchronous nature of AJAX? When you do AJAX you should get accustomed to work with events and callbacks instead of writing sequential code. You can't return the inner contents. The simple reason for this is that this inner function could execute much later than the outer function. So the outer function will return a result much before the success callback executes. So here's the correct way: function get_users() { $.post( url_base + 'travel/trip/get_users/' + trip_id, function(response) { // instead of trying to return anything here // simply do something with the response // Depending on what the server sent you there // will be different ways. // Here you could also call some other custom function // and pass it the response } 'json' ); }

Related questions

0 votes
    It's probably obvious to you, but I can't figure it out. I need to make function that returns it ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    The ________ function returns its argument with a modified shape, whereas the ________ method modifies the array ... answers pdf, Data Science interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    The ________ function returns its argument with a modified shape, whereas the ________ method modifies the array itself. (a) ... (c) reshape2, resize (d) all of the mentioned...
asked Oct 7, 2021 in Technology by JackTerrance
0 votes
    Why is the span length ratio of end span/approach span to its neighboring inner spans usually about 0.75?...
asked Jan 3, 2021 in Technology by JackTerrance
0 votes
    Storage that returns its data after the power is turned off is referred' to as- (a) Sequential storage (b) ... (e) Volatile storage Select the correct answer from above options...
asked Dec 11, 2021 in Education by JackTerrance
0 votes
    I am trying to make an app that can get the patient's symptoms as inputs and outputs the three most likely ... classes[sortedshit[2]]) Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    What is true about functions : I) Functions are objects II) Can be assigned to a variable III) Can be anonymous IV) Return value type has ... , III 3. I, II, III, IV 4. I, II, IV...
asked Feb 25, 2021 in Technology by JackTerrance
0 votes
    What is true about functions : I) Functions are objects II) Can be assigned to a variable III) Can be anonymous IV) Return value type has ... , III C) I, II, III, IV D) I, II, IV...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    title basically. The question says create a function that takes in a string and returns true when the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    title basically. The question says create a function that takes in a string and returns true when the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    To create sequences of numbers, NumPy provides a function __________ analogous to range that returns arrays instead ... answers pdf, Data Science interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    _______________ allows a system administrator to associate a function with a relation; the function returns a ... Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Which of the following function of Array object removes the last element from an array and returns that element? push() delete() pop() link()...
asked Dec 24, 2020 in Technology by JackTerrance
0 votes
    I am getting empty row on following sql SELECT * FROM flyers WHERE fId='6' AND 'userId'='400' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
+1 vote
    What it the return value of a relational operator if it returns any in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
...