request.execute(function(resp){}); 是什么意思?在 Javascript?
What is the meaning of request.execute(function(resp){}); in Javascript?
用例是调用(Google 驱动器)API 以获取数据。我了解执行请求的第一部分。但是为什么我们要将 response
传递给函数呢?如果有的话,我们不会得到回复吗?
是否有很好的资源来理解请求/响应以及服务器在概念上是如何工作的?或者谁能post打个比方?
如何获取响应的属性?
What is the meaning of request.execute(function(resp){});
它调用request.execute
,传入一个由函数表达式function(resp){}
创建的函数。该示例中的函数不执行任何操作。
But why are we passing a response into the function?
我们不是。
If anything, wouldn't we get a response back?
是的,这就是代码的作用。您正在传递一个您写入 request.execute
的函数,它 将使用给出响应的参数调用该函数。该函数接受该响应作为 resp
,您可以在代码中使用它。
传递到 execute
的函数称为 回调,因为 execute
将在响应可用时回调它。这是您要搜索的术语以查找教程等。另外(因为该示例几乎肯定是异步的)您将要查找 "asynchronous programmming" 和类似的。
下面是一个异步调用回调函数的简单示例:
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.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
并非所有回调都是异步的。例如,你给 Array#sort
的回调是同步的:
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.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
用例是调用(Google 驱动器)API 以获取数据。我了解执行请求的第一部分。但是为什么我们要将 response
传递给函数呢?如果有的话,我们不会得到回复吗?
是否有很好的资源来理解请求/响应以及服务器在概念上是如何工作的?或者谁能post打个比方?
如何获取响应的属性?
What is the meaning of request.execute(function(resp){});
它调用request.execute
,传入一个由函数表达式function(resp){}
创建的函数。该示例中的函数不执行任何操作。
But why are we passing a response into the function?
我们不是。
If anything, wouldn't we get a response back?
是的,这就是代码的作用。您正在传递一个您写入 request.execute
的函数,它 将使用给出响应的参数调用该函数。该函数接受该响应作为 resp
,您可以在代码中使用它。
传递到 execute
的函数称为 回调,因为 execute
将在响应可用时回调它。这是您要搜索的术语以查找教程等。另外(因为该示例几乎肯定是异步的)您将要查找 "asynchronous programmming" 和类似的。
下面是一个异步调用回调函数的简单示例:
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.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
并非所有回调都是异步的。例如,你给 Array#sort
的回调是同步的:
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.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>