0

my javascript:

function getCurrentTabId(){
    chrome.tabs.query({active:true,windowType:"normal", currentWindow: true},function(d){
      alert( d[0].id ); // returns expected result '2'
      return d[0].id;
    });
  }

  alert( getCurrentTabId() ); // returns 'undefined'

How do I get the last line "alert( getCurrentTabId() )" to display 2 instead of undefined?

JasonH
  • 521
  • 1
  • 8
  • 19
  • 3
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Josh Lee Dec 01 '17 at 22:13
  • Callbacks in chrome.* API are invoked asynchronously, see the linked duplicate thread. – wOxxOm Dec 01 '17 at 22:22
  • I can see that I have a problem with an asynchronous call, but I don't see how to fix it... – JasonH Dec 01 '17 at 22:33
  • Rework the code as shown in that answer and other tutorials for asynchronous javascript. In short, chain the callbacks, chain the Promise or use async/await. – wOxxOm Dec 02 '17 at 01:26

0 Answers0