Welcome guest, is this your first visit? Create Account now to join.
Results 1 to 2 of 2

This is a discussion on Problems with callback execution order within the Plugins Development section, part of the Chrome Plugins category: Does anyone know an elegant way of making this function return the correct value? It is meant to return 1, ...


  1. #1
    twinsen is offline Member
    Join Date
    Aug 2009
    Posts
    31

    Default Problems with callback execution order

    Does anyone know an elegant way of making this function return the correct value?
    It is meant to return 1, since there is one entry of "g_sampleSettingChildName".

    Code:
    function getNumBookmarkFolders()
    {
    	log('getNumBookmarkFolders() Begin');
    	var numBookmarkFolders = -1;
    	chrome.bookmarks.search(g_sampleSettingChildName, function(results)
    	{
    		log('callback function begin');
    		numBookmarkFolders = results.length;
    		log("If we returned here: "+numBookmarkFolders);
    		return numBookmarkFolders;
            });
    	log('getNumBookmarkFolders() End');
    	log("getNumBookmarkFolders() returning: "+numBookmarkFolders);
    	return numBookmarkFolders;
    }
    I click on a button:
    Code:
    <input id="debugShowNumBookmarkFolders" type="button" value="debugShowNumBookmarkFolders" onclick="alert(window.opener.getNumBookmarkFolders());" />
    It shows:
    getNumBookmarkFolders() Begin
    getNumBookmarkFolders() End
    getNumBookmarkFolders() returning: -1
    callback function begin
    If we returned here: 1
    And displays -1 in the alert box. As you can see the getNumBookmarkFolders() function exits before the callback has finished. Is there an elegant way of making getNumBookmarkFolders() wait until the callback has finished?

    Changing to this:
    Code:
    function getNumBookmarkFolders()
    {
    	var callbackFinished = false;
    	log('getNumBookmarkFolders() Begin');
    	var numBookmarkFolders = -1;
    	chrome.bookmarks.search(g_sampleSettingChildName, function(results)
    		{
    			log('callback function begin');
    			numBookmarkFolders = results.length;
    			log("If we returned here: "+numBookmarkFolders);
    			callbackFinished = true;
            });
    	while (!callbackFinished)
    	{
    		; //maybe sleep?
    	}
    	log('getNumBookmarkFolders() End');
    	log("getNumBookmarkFolders() returning: "+numBookmarkFolders);
    	return numBookmarkFolders;
    }
    Just displays:
    getNumBookmarkFolders() Begin

    And gets stuck in an infinite loop. The callback never gets executed.
    The only way I know of is to nest whatever you want to happen "after" the callback inside the callback (eg you can even pass in a function pointer). Is there some way of waiting for a callback to finish?

  2. #2
    twinsen is offline Member
    Join Date
    Aug 2009
    Posts
    31

    Default

    This is an example of callback chaining:

    function getNumBookmarkFolders(afterGetNumBookmarkFolders)
    {
    var numBookmarkFolders = -1;
    chrome.bookmarks.search(g_sampleSettingChildName, function(results)
    {
    numBookmarkFolders = results.length;
    afterGetNumBookmarkFolders(numBookmarkFolders);
    });
    }
    function afterGetNumBookmarkFolders(numBookmarkFolders)
    {
    alert(numBookmarkFolders);
    }
    <input id="debugShowNumBookmarkFolders" type="button" value="debugShowNumBookmarkFolders" onclick="window.opener.getNumBookmarkFolders(afterGetNumBookmarkFolders);" />
    I'd prefer to be able to do "var num = getNumBookmarkFolders();". Is that even possible?

Similar Threads

  1. Toolstrip Order?
    By devilslackey in forum Chrome Plugins
    Replies: 1
    Last Post: 06-11-2009, 07:48 PM
  2. pogo problems
    By needa in forum Bugs and Vulnerabilities
    Replies: 0
    Last Post: 02-18-2009, 05:52 PM
  3. Page loading problems...please help...
    By walking101 in forum Bugs and Vulnerabilities
    Replies: 0
    Last Post: 01-21-2009, 04:53 PM
  4. Bookmark problems
    By snadge in forum Chrome Troubleshooting
    Replies: 1
    Last Post: 11-10-2008, 07:08 PM
  5. Problems With GoogleChrome
    By googlefan in forum Chrome Troubleshooting
    Replies: 1
    Last Post: 10-06-2008, 11:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •