开发者

callback function data handling

开发者 https://www.devze.com 2023-02-22 02:11 出处:网络
im not very familiar with callback functions in javascript and how they handle data. i want to achieve something like this in google chrome

im not very familiar with callback functions in javascript and how they handle data.

i want to achieve something like this in google chrome

function getBookmarkBar()
{
  chrome.bookmarks.getChildren('1',function(bookmarkNodes)
  {
     return bookmarkNodes;
  });
}

function getOtherBookmarks(folderId)
{
  chrome.bookmarks.getChildren(folderId,function(bookmarkNodes)
  {
    return bookmarkNodes;
  });
}

function doprocessing(){
{
  bookmarkbarNodes=getBookmarkBar();
  otherNodes=getOtherBookmarks('2');

 //do some processing for bookmarkbarNodes and otherNodes
}

is there a way possible(or as close as possible) to do this ? right now i do this by a single function but that would be a bad idea because of coupling of retrieval and modification logic:

function process{
 chrome.boomarks.getChildren(开发者_Go百科'1',function(bookmarkNodes){
   chrome.bookmarks.getChildren('2',function(otherNodes){
     //do processing
    });
});

writing above code in generic function would make it highly reusable.


There is no way to synchronize functions. Also, it is a bad idea to do that becaus you would block the program until the function is fully executed.

Callbacks are a great thing. There is a good reason why the API is totally asynchronus. They don't want you to synchronize the functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消