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 8128799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:02:47+00:00 2026-06-06T08:02:47+00:00

Pretty sure this has been asked already, but I don’t know what to search

  • 0

Pretty sure this has been asked already, but I don’t know what to search for. Anyway,

var livemarks = [];

var livemarkIds = PlacesUtils.annotations.getItemsWithAnnotation("livemark/feedURI", {});

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

    PlacesUtils.livemarks.getLivemark( {id : livemarkIds[i]}, function(result, livemark){

        if (result == Components.results.NS_OK){
            livemarks.push(livemark);
        }

    });

}

alert(livemarks.length);

I am trying to play a bit with a Firefox addon that’s no longer maintained by its creator, just to learn a bit. I recently got an error saying getFeedURI is going to be deprecated and I want to change his old function.

EDIT:

From a function defined in a function (inner function), I am unable to access a var defined in the parent. Why?
E.g. I cannot access var livemarks from inside getLivemark(), or other similar internal functions.

I was checking (scroll down completely): this and his code works fine. So what’s wrong with my code? I just wanted to avoid the recursion, if possible.

  • 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-06T08:02:50+00:00Added an answer on June 6, 2026 at 8:02 am

    I suspect the PlacesUtils.livemarks.getLivemark function does its work asynchronously, so your callback is called after you alert the length. Put your alert inside the callback and you should see the correct length (eventually). Here’s one way:

    var expecting = livemarkIds.length;
    for (var i = 0; i < livemarkIds.length; i++){
    
        PlacesUtils.livemarks.getLivemark( {id : livemarkIds[i]}, function(result, livemark){
    
            if (result == Components.results.NS_OK){
                livemarks.push(livemark);
    
                // ***New stuff***
                if (livemarks.length === expecting) {
                    // Now you have them all, now you can do the next thing
                    doSomethingWithTheLiveMarks(livemarks);
                }
            }
    
        });
    }
    

    Note that there I put livemarkIds.length into expecting, just in case you do other things with livemarkIds while the function is running. If you aren’t, you can just use that directly.


    Re your comment below:

    However, the system works like this: I get the livemarks in an array. This code is in a class (and method) actually, so another class initializes this one and will call the function getFeeds(), which will return that array of livemarks.

    If PlacesUtils.livemarks.getLivemark is asynchronous, it’s impossible for getFeeds to return the array as a return value. E.g., it cannot be used like this:

    a = b;
    c = 42;
    feeds = getFeeds(feedIds);
    if (feeds.length === 0) {
        // Do something
    }
    else {
        // Do something else
    }
    

    The good news is it’s really easy to fix: Have getFeeds accept a callback function that it calls when it has the feeds. The code above changes to look like this:

    a = b;
    c = 42;
    feeds = getFeeds(feedIds, function(feeds) {
        if (feeds.length === 0) {
            // Do something
        }
        else {
            // Do something else
        }
    });
    

    As you can see, it’s a pretty straightforward change. Assuming the loop above is all of getFeeds, then getFeeds ends up looking something like this:

    function getFeeds(livemarkIds, callback) {
    
        var livemarks = [];
        for (var i = 0; i < livemarkIds.length; i++){
    
            PlacesUtils.livemarks.getLivemark( {id : livemarkIds[i]}, function(result, livemark){
    
                if (result == Components.results.NS_OK){
                    livemarks.push(livemark);
    
                    if (livemarks.length === livemarkIds.length) {
                        // Done, trigger the callback
                        callback(livemarks);
                    }
                }
    
            });
        }
    }
    

    And the pattern continues: If the code calling getFeeds is being called by something else that’s expecting a return value from the async stuff, instead of returning that value, you have that code accept a callback, and call the callback from the getFeeds callback. And so on.

    Once you get used to it, it’s very easy to do. Getting used to it can be tricky. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty sure it hasn't, but apologies if this question has already been asked.
I'm pretty sure this has been asked before but I can't seem to find
I'm pretty sure this has been asked before, but I can't for the life
I am pretty sure this question has been asked before, but I could not
I am pretty sure that this question has been asked before, but my case
I'm sure this has either been asked before or is pretty straightforward. But for
I'm sure this has either been asked before or is pretty straightforward. But for
i'm pretty sure that this question has allready been asked (but i didn't find
Sorry if this has already been asked, but I just want to make sure
im pretty sure this question anywhere already has been asked anywhere - didnt found

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.