-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JavaScript:回调是什么鬼? #1792
JavaScript:回调是什么鬼? #1792
Conversation
校对认领 @sqrthree |
@sqrthree 校对认领 |
|
||
![](https://cdn-images-1.medium.com/max/1000/1*pWGJIKats-zuumA3RQNEWQ.jpeg) | ||
|
||
Callbacks — image via [unsplash](https://unsplash.com/search/call?photo=qXn5L9BqRbE) | ||
回调 — 题图来自 [unsplash](https://unsplash.com/search/call?photo=qXn5L9BqRbE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
破折号是不是 ——
|
||
### Why do we need Callbacks? | ||
^ 这听起来有点罗嗦,让我们来看一些例子来简化一下。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
罗嗦 => 啰嗦
|
||
A callback is important here because we need to wait for a response from the server before we can move forward in our code. We don’t know if our API request is going to be successful or not so after sending our parameters to search/tweets via a get request, we wait. Once Twitter responds, our callback function is invoked. Twitter will either send an `err` (error) object or a `response` object back to us. In our callback function we can use an `if()` statement to determine if our request was successful or not, and then act upon the new data accordingly. | ||
回调在这里很重要,因为在我们的代码继续运行之前我们需要等待一个来自服务端的响应。我们并不知道 API 请求会成功或者失败,所以通过 get 向 search/tweets 发送了请求参数以后,我们要等待。一旦 Twitter 响应,我们的回调函数就被调用。Twitter 要么发送一个 `err`(error)对象,要么发送一个 `response` 对象返回给我们。在我们的回调函数中我们可以使用 `if()` 声明来区分请求是否成功,然后相应地处理新数据。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我们并不知道 API 请求会成功或者失败 => 我们并不知道 API 请求会成功还是会失败
|
||
A callback is important here because we need to wait for a response from the server before we can move forward in our code. We don’t know if our API request is going to be successful or not so after sending our parameters to search/tweets via a get request, we wait. Once Twitter responds, our callback function is invoked. Twitter will either send an `err` (error) object or a `response` object back to us. In our callback function we can use an `if()` statement to determine if our request was successful or not, and then act upon the new data accordingly. | ||
回调在这里很重要,因为在我们的代码继续运行之前我们需要等待一个来自服务端的响应。我们并不知道 API 请求会成功或者失败,所以通过 get 向 search/tweets 发送了请求参数以后,我们要等待。一旦 Twitter 响应,我们的回调函数就被调用。Twitter 要么发送一个 `err`(error)对象,要么发送一个 `response` 对象返回给我们。在我们的回调函数中我们可以使用 `if()` 声明来区分请求是否成功,然后相应地处理新数据。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if()
声明 => if()
语句
|
||
Good work! You can now (ideally) understand what a callback is and how it works. This is merely the tip of the iceberg with callbacks, there is still a lot more to learn! I publish a few articles/tutorials each week, please [enter your email here](https://docs.google.com/forms/d/e/1FAIpQLSeQYYmBCBfJF9MXFmRJ7hnwyXvMwyCtHC5wxVDh5Cq--VT6Fg/viewform) if you’d like to be added to my once-weekly email list. | ||
干的漂亮!你现在(理想状况下)已经理解了回调是什么,回调如何工作。这只是回调的冰山一角,记住学无止境啊!我每周都会更新一些文章/教程,如果你愿意接收每周一次的推送,[点击这里](https://docs.google.com/forms/d/e/1FAIpQLSeQYYmBCBfJF9MXFmRJ7hnwyXvMwyCtHC5wxVDh5Cq--VT6Fg/viewform)输入你的邮箱订阅吧 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
干的漂亮 => 干得漂亮
|
||
Good work! You can now (ideally) understand what a callback is and how it works. This is merely the tip of the iceberg with callbacks, there is still a lot more to learn! I publish a few articles/tutorials each week, please [enter your email here](https://docs.google.com/forms/d/e/1FAIpQLSeQYYmBCBfJF9MXFmRJ7hnwyXvMwyCtHC5wxVDh5Cq--VT6Fg/viewform) if you’d like to be added to my once-weekly email list. | ||
干的漂亮!你现在(理想状况下)已经理解了回调是什么,回调如何工作。这只是回调的冰山一角,记住学无止境啊!我每周都会更新一些文章/教程,如果你愿意接收每周一次的推送,[点击这里](https://docs.google.com/forms/d/e/1FAIpQLSeQYYmBCBfJF9MXFmRJ7hnwyXvMwyCtHC5wxVDh5Cq--VT6Fg/viewform)输入你的邮箱订阅吧 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
输入你的邮箱订阅吧 => 输入你的邮箱订阅吧!
漏了个标点
@@ -2,29 +2,30 @@ | |||
> * 原文作者:[Brandon Morelli](https://codeburst.io/@bmorelli25) | |||
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) | |||
> * 译者: | |||
> * 校对者: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
校对者:wilsonandusa
For one very important reason — JavaScript is an event driven language. This means that instead of waiting for a response before moving on, JavaScript will keep executing while listening for other events. Lets look at a basic example: | ||
### 为什么我们需要回调? | ||
|
||
有一个非常重要的原因 —— JavaScript 是事件驱动的语言。这意味着,在继续运行之前,JavaScript 不会等待响应,而是在监听其他事件时继续执行。来看一个基本的例子: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"在继续运行之前,JavaScript 不会等待响应" -> "JavaScript不会因为要等待一个响应而停止当前运行"
|
||
T.get('search/tweets', params, function(err, data, response) { | ||
if(!err){ | ||
// This is where the magic will happen | ||
// 这里是魔法将要发生之处 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[这里是魔法将要发生之处 ]->[这里就是神奇之处] (这样感觉通俗一点)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是一个将来写主要代码的地方,目前只是占位,所以目前不见得有什么神奇。 打算翻译成:这里是施展魔法之处
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌好的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sunui 已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。 |
@sqrthree #1785 翻译完成