fix some translate error (#34138)

This commit is contained in:
Johnson
2019-02-10 14:06:59 +08:00
committed by Jingyi Ding
parent 320f4c3637
commit c47a7f0bf1

View File

@ -1,34 +1,42 @@
--- ---
title: Await Promises title: Await Promises
localeTitle: 等待承诺 localeTitle: Await Promise
--- ---
## 等待承诺 ## Await Promise
`async` / `await` [运算符](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators)可以更轻松地实现许多异步Promise。它们还允许工程师编写更清晰,更简洁,可测试的代码。 `async` / `await` [关键字](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators)可以更轻松地实现许多异步Promise。它们能帮助工程师编写更清晰,更简洁,可测试的代码。
要理解这个主题,您应该对[Promise](https://guide.freecodecamp.org/javascript/promises)如何工作有充分的了解。 要理解这个主题,您需要对[Promise](https://guide.freecodecamp.org/javascript/promises)的工作机制有充分的了解。
* * * * * *
## 基本语法 ## 基本语法
\`\`\`\`javascript function slowlyResolvedPromiseFuncstring{ 返回新的Promiseresolve => { setTimeout=> { 解析(字符串); }5000; }; } ```javascript
function slowlyResolvedPromiseFunc (string) {
return new Promiseresolve => {
setTimeout() => { resolvestring}, 5000;
};
}
异步函数doIt{ const myPromise = await slowResolvedPromiseFunc“foo”; 的console.logmyPromise; //“foo” } async function doIt () {
const myPromise = await slowResolvedPromiseFunc ('foo');
console.log(myPromise); // 'foo'
}
doIt方法(); doIt();
``` ```
There are a few things to note: 有几点需要注意:
* The function that encompasses the `await` declaration must include the `async` operator. This will tell the JS interpreter that it must wait until the Promise is resolved or rejected. * 包含`await`关键字的函数在定义时必须有`async`关键字修饰. 它会阻塞javascript进程直到Promise执行了resolve或者reject
* The `await` operator must be inline, during the const declaration. * `await`关键字必须和声明的变量在同一行。
* This works for `reject` as well as `resolve`. *`reject``resolve`效果相同。
--- ---
## Nested Promises vs. `Async` / `Await` ## 嵌套 Promises vs. `Async` / `Await`
Implementing a single Promise is pretty straightforward. In contrast, Chained Promises or the creation of a dependency pattern may produce "spaghetti code". 实现一个Promise很简单。 然而链式的Promise或有依赖的模式会导致“意大利面条”式的代码。
The following examples assume that the <a href='https://github.com/request/request-promise' target='_blank' rel='nofollow'>`request-promise`</a> library is available as `rp`. The following examples assume that the <a href='https://github.com/request/request-promise' target='_blank' rel='nofollow'>`request-promise`</a> library is available as `rp`.