P
P
Pasha Kurbatsky2014-05-20 09:02:38
JavaScript
Pasha Kurbatsky, 2014-05-20 09:02:38

Why do google chrome api methods return undefined?

Hello, I'm interested in developing extensions for chrome. I read the documentation, practiced a little and immediately a problem arose.

function getUrl(tabId) {
  
  chrome.tabs.get(tabId, function(data) {
    return data.url ; 
  }) ; 
}

Why does this code return undefined?
I tried that, but the result is the same.
var getUrl = function(tabId) {
    var url ; 

    chrome.tabs.get(tabId, function(data) {

      url = data.url ; 

    }) ; 

    return url ; 
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Solovyov, 2014-05-20
@pavel_salauyou

function(data) - is processed asynchronously and therefore you cannot get a result from it using return

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question