0

I am building a chrome extension and got stuck here. It's a simple javascript problem but since I am no expert in javascript, any help would be appreciated.

function active()
{
   chrome.extension.sendRequest({cmd:"mycommand"}, function(callback)
   {
       if(callback.mydata)
       {
           return true;
       }
       else
       {
          return false;
       }
   });
}

console.log(active());    //prints undefined

I know that if it had been a simple nested function case, I'd need to call the inner one first, like

function active()
{
    function inner()
    {
         //some task
         return true;
    }
    return inner();
}

console.log(active());   //works

But I've never come across a case as I'm facing now.

aandis
  • 4,084
  • 4
  • 29
  • 40
  • Seems like first active function is asynchronous so you can't use it like it's synchronous. You could subscribe to an event which will be emitted from class containing active method, but mixing synchronous with async might be the issue here. – Daniel Kmak May 23 '14 at 19:50
  • This is because you're not returning anything in the function `active`. It's simply calling `chrome.extension.sendRequest` which I'm assuming does something asynchronously, then invokes the callback function that you passed as the second argument, hence why it prints `undefined`. – Austin Brunkhorst May 23 '14 at 19:51
  • @Daniel yes. You are right. The sendrequest method is indeed asynchronus. – aandis May 23 '14 at 19:53
  • @AustinBrunkhorst I realize that. So how do I make it work? – aandis May 23 '14 at 19:54
  • Like this -> http://jsfiddle.net/6ZUzh/ – adeneo May 23 '14 at 19:56
  • possible duplicate of [Why is my variable undefined after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-undefined-after-i-modify-it-inside-of-a-function-asynchron) – rsanchez May 24 '14 at 09:01

1 Answers1

0
chrome.extension.sendRequest({cmd:"mycommand"}, function(callback)
   {
       if(callback.mydata)
       {
           return true;
       }
       else
       {
          return false;
       }
   });

This is an asynchronous request. Read this for more info http://msdn.microsoft.com/en-us/library/windows/apps/hh700330.aspx To make it work, you will have to use callback method like this:

 function active(callbackToLogResult) {

 chrome.extension.sendRequest({cmd:"mycommand"}, function(callback)
 {
     if(callback.mydata)
     {
         callbackToLogResult(true);
     }
     else
     {
         callbackToLogResult(false);
     }
 });
 }

 active(function(result){
     console.log(result);
 });
Cute_Ninja
  • 4,742
  • 4
  • 39
  • 63
  • 1
    Mind adding more general explanations and/or a link to tutorial on async javascript? – Xan May 23 '14 at 19:55
  • More explanation on callbacks and asynchronous functions http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/ – Cute_Ninja May 23 '14 at 20:00
  • I actually intended to use the active function later as if(active()) { //dosomething } How would I go about that with your solution without using globals? – aandis May 23 '14 at 20:03
  • Like this: active(function(result) {if (result) {// do something} else {//don't do something}}); – Cute_Ninja May 23 '14 at 20:50