最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is the meaning of request.execute(function(resp){}); in Javascript? - Stack Overflow

programmeradmin1浏览0评论

The use case is a call to the (Google Drive) API to get data. I understand the first part of executing the request. But why are we passing a response into the function? If anything, wouldn't we get a response back?

Are there good resources for understanding requests/ responses and how servers work conceptually? Or could someone post an analogy?

How do I get properties of the response back?

The use case is a call to the (Google Drive) API to get data. I understand the first part of executing the request. But why are we passing a response into the function? If anything, wouldn't we get a response back?

Are there good resources for understanding requests/ responses and how servers work conceptually? Or could someone post an analogy?

How do I get properties of the response back?

Share Improve this question edited Jul 17, 2015 at 8:19 Adrienne asked Jul 17, 2015 at 8:12 AdrienneAdrienne 2,6903 gold badges32 silver badges39 bronze badges 2
  • 1 you don't pass it the response, it does. You pass it a function that gets passed the response by google. an analogy is those advertisements where they tell you to "send us a self-addressed stamped envelope for your free brochure": you give them the envelope, they put the brochure in the envelope and drop it in the mail, you get it sometime later... – dandavis Commented Jul 17, 2015 at 8:14
  • "Are there good resources for understanding requests/ responses and how servers work conceptually?" Just FYI, that part is off-topic for SO: "Questions asking us to remend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam." – T.J. Crowder Commented Jul 17, 2015 at 8:26
Add a ment  | 

1 Answer 1

Reset to default 5

What is the meaning of request.execute(function(resp){});

It calls request.execute, passing in a function created by the function expression function(resp){}. The function in that example doesn't do anything.

But why are we passing a response into the function?

We aren't.

If anything, wouldn't we get a response back?

Yes, and that's what the code does. You're passing a function you write into request.execute, which it will call with an argument giving the response. The function accepts that response as resp and you can use it in your code.

The function being passed into execute is called a callback, because execute will call it back when the response is available. That's the term you want to search for to find tutorials, etc. Also (as that example is almost certainly asynchronous) you'll want to look for "asynchronous programmming" and similar.

Here's a simple example of a function that calls a callback asynchronously:

function execute(callback) {
  setTimeout(function() {
    callback(Math.floor(Math.random() * 100));
  }, 500);
}

snippet.log("Calling execute");
execute(function(resp) {                                // <= This is the part
  snippet.log("Got callback, resp = " + resp);          // <= like your
});                                                     // <= example
snippet.log("Done calling execute, waiting for callback");
<!-- Script provides the `snippet` object, see http://meta.stackexchange./a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Not all callbacks are asynchronous. For instance, the callback you give Array#sort is synchronous:

var a = [1, 7, 3, 42];

snippet.log("Calling Array#sort, array is: " + a.join(", "));
a.sort(function(a, b) {                                   // <= This is
  snippet.log("Comparing " + a + " and " + b);            // <= the part
  return b - a;                                           // <= like your
});                                                       // <= example
snippet.log("Done calling Array#sort, array is now: " + a.join(", "));
<!-- Script provides the `snippet` object, see http://meta.stackexchange./a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

发布评论

评论列表(0)

  1. 暂无评论