Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You will need to declare the variable in a scope where all functions can access it. As your <code>getGroupMembers</code> function is in the global scope, so your <code>users</code> variable needs to be. Move it into the <code>ready</code> or <code>click</code> scope, and you can declare the variable as local.</p> <p>However, it would be much easier to pass the result of your request as arguments to <code>resolve</code>:</p> <pre><code>$(document).ready(function() { $('#message_send').click(function() { var deferred = getGroupMembers({ page: 1, per_page: 100 }); deferred.then(function(users) { console.log(users); }); }); }); function getGroupMembers(querydata) { var users = []; var deferredObject = new $.Deferred(); … // some asynchronous tasks, callbacking either deferredObject.resolve(users); // or deferredObject.reject(); … return deferredObject.promise(); } </code></pre> <p>For some syntactic sugar, you might as well just use the <a href="http://api.jquery.com/deferred.pipe/" rel="nofollow"><code>pipe</code> method</a> of the Ajax Deferred.</p> <hr> <p>Recursive piped method:</p> <pre><code>function getGroupMembers(querydata) { return $.ajax({ url: "/echo/json/", dataType: "json", data: querydata, cache: false }).pipe(function(data) { var user = data.response.UserActions, users = []; for (var u = 0; u &lt; user.length; u++) { users.push(user[u].user.id); } if (querydata.page &lt; data.meta.total_pages) { querydata.page++; return getGroupMembers(querydata).pipe(function(nextusers) { return users.concat(nextusers); }); } else { return users; } }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload