From c47a7f0bf1f331a7308cc387b1dfcdb3fe33abc9 Mon Sep 17 00:00:00 2001 From: Johnson Date: Sun, 10 Feb 2019 14:06:59 +0800 Subject: [PATCH] fix some translate error (#34138) --- .../javascript/await-promises/index.md | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/guide/chinese/javascript/await-promises/index.md b/guide/chinese/javascript/await-promises/index.md index f2e63444ed..645e5fea3b 100644 --- a/guide/chinese/javascript/await-promises/index.md +++ b/guide/chinese/javascript/await-promises/index.md @@ -1,34 +1,42 @@ --- 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 slowlyResolvedPromiseFunc(string){ 返回新的Promise(resolve => { setTimeout(()=> { 解析(字符串); },5000); }); } +```javascript +function slowlyResolvedPromiseFunc (string) { + return new Promise(resolve => { + setTimeout(() => { resolve(string}, 5000; + }; +} -异步函数doIt(){ const myPromise = await slowResolvedPromiseFunc(“foo”); 的console.log(myPromise); //“foo” } - -doIt方法(); -``` -There are a few things to note: +async function doIt () { + const myPromise = await slowResolvedPromiseFunc ('foo'); + console.log(myPromise); // 'foo' + } - * 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. - * The `await` operator must be inline, during the const declaration. - * This works for `reject` as well as `resolve`. +doIt(); +``` +有几点需要注意: + + * 包含`await`关键字的函数在定义时必须有`async`关键字修饰. 它会阻塞javascript进程,直到Promise执行了resolve或者reject。 + * `await`关键字必须和声明的变量在同一行。 + * 对`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 `request-promise` library is available as `rp`. @@ -89,4 +97,4 @@ errorExample(); \`\`\` #### 更多信息: * `await`运营商[MDN文档](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) -* `async`功能操作员[MDN文档](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/async_function) \ No newline at end of file +* `async`功能操作员[MDN文档](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/async_function)