Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8271129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:45:59+00:00 2026-06-08T06:45:59+00:00

I’ve been trying to understand Javascript closures, and thought I was onto something. I

  • 0

I’ve been trying to understand Javascript closures, and thought I was onto something. I have managed to successfully return an iterated integer (generated by a for loop) within my inner function.

However, this function in question also takes a parameter generated by the function itself. Forgive my terminology – but it may be a callaback parameter (basically, the parameter’s value is returned based on the function I send out).

There is more explanation of my problem below, but first of all, here is the code in question (with the closure working – [value] is a changing value of i for each iteration):

FB.api('/me/tagged?limit=100', function(response) {

for (var i=0; i<response.data.length; i++){

    var taggedId = response.data[i].id;
    var fromId = response.data[i].from.id;
    var fromName = response.data[i].from.name;

    var taggedQuery = new Parse.Query('tagged');
    taggedQuery.equalTo('taggedId',taggedId);
    taggedQuery.first({ //.first returns the first match (.find returns all)
        success: (function(value) {
            return function() {
            console.log(response.data[value]);
            }
        })(i),
        error: function(taggedRecError){
            console.log('error: '+taggedRecError.message);
        }
   });
} //for
});

So basically, in the above, I’m querying Facebook to get the user’s tagged posts. I use a for loop to iterate through each result.

I then use Parse.js to query whether I have a record saved for this tagged object.

I want my code to recognise whether or not I do (have a saved object). In Parse I would usually do this using something like the following:

taggedQuery.first({ //.first returns the first match (.find returns all)
        success: function(value) {
                if(value){
                   //do nothing
                 }
                else {
                   //do something - e.g. save to Parse data store
                }

        }
});

So, as you can see, I use the existence of a returned paramater (value) in order to decide what to do next.

However, in the first code block, you can see that in order to get the closure working I have to pass a parameter (‘(i)’) directly into the success function in order to get the appropriate value of i for use within the function. However, this then distorts the result we get from Parse – i.e. the returned object comes from me passing in a parameter – not from Parse returning one based on my data store

I seem to be in a catch 22: pass in a parameter manually to get successful closure (but then lose the returned paramater from the Parse call), or pass in no parameter to ensure Parse returns the correct response based on my data stores (but then have the wrong value of i within said function: i.e. – no closure).

I hope that’s clear. If not please let me know. Any help appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:46:01+00:00Added an answer on June 8, 2026 at 6:46 am

    You’re on the right track, this is what I think you want to do with your success parameter:

    taggedQuery.first({ //.first returns the first match (.find returns all)
        success: (function(index) {
            return function(value) {
                //value is what Parse passes
                //index is your preserved index
            };
        }(i))
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.