0

I'm having trouble using the chrome.commands.getAll API in creating a Chrome Extension. The main function returns before the closure is executed meaning the return value is wrong.

Is it possible to synchronise this without an external library?

var find = function()
{
    var shortcut = null;

    chrome.commands.getAll(function(commands){
        //Assigning the shortcut (more stuff happens in here in the actual code - I have shortened it for simplicity)
        shortcut = 'foo';
    });

    return shortcut;
};
Tom
  • 950
  • 3
  • 15
  • 27
  • possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Xan May 14 '15 at 14:54
  • 1
    You can't do that. http://blog.slaks.net/2015-01-04/async-method-patterns/ – SLaks May 14 '15 at 14:54
  • No, it's not possible. But the better way is to figure out how to make your program work asynchronously. After all, the entire point of asynchronicity is that `getAll` could take 40 minutes to return while Bob gets the appropriate information. – Teepeemm May 14 '15 at 18:35

0 Answers0