Skip to content

Commit 472bbcc

Browse files
committed
Concurrency
1 parent 72d2bd8 commit 472bbcc

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

README.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Inspired from [clean-code-javascript](https://github.com/ryanmcdermott/clean-cod
3737

3838
## Introduction
3939

40+
## 简介
41+
4042
![Humorous image of software quality estimation as a count of how many expletives
4143
you shout when reading code](https://www.osnews.com/images/comics/wtfm.jpg)
4244

@@ -63,8 +65,6 @@ shaped into its final form. Finally, we chisel away the imperfections when
6365
we review it with our peers. Don't beat yourself up for first drafts that need
6466
improvement. Beat up the code instead!
6567

66-
## 简介
67-
6868
![一张用你阅读代码时吐槽的数量来评估软件质量的搞笑图片](https://www.osnews.com/images/comics/wtfm.jpg)
6969

7070
将源自 Robert C. Martin 的 [*Clean Code*](https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882)
@@ -85,7 +85,9 @@ improvement. Beat up the code instead!
8585

8686
**[⬆ 返回目录](#目录)**
8787

88-
## Variables 变量
88+
## Variables
89+
90+
## 变量
8991

9092
### Use meaningful variable names
9193

@@ -311,7 +313,9 @@ function loadPages(count: number = 10) {
311313

312314
**[⬆ 返回目录](#目录)**
313315

314-
## Functions 函数
316+
## Functions
317+
318+
## 函数
315319

316320
### Function arguments (2 or fewer ideally)
317321

@@ -1230,7 +1234,7 @@ function print(n: number) {
12301234
print(10);
12311235
```
12321236

1233-
**Good:**
1237+
**好的:**
12341238

12351239
```ts
12361240
// Generates an infinite stream of Fibonacci numbers.
@@ -2454,13 +2458,19 @@ describe('Calendar', () => {
24542458

24552459
## Concurrency
24562460

2461+
## 并发
2462+
24572463
### Prefer promises vs callbacks
24582464

2465+
### 倾向于 Promise 而不是回调
2466+
24592467
Callbacks aren't clean, and they cause excessive amounts of nesting *(the callback hell)*.
24602468
There are utilities that transform existing functions using the callback style to a version that returns promises
24612469
(for Node.js see [`util.promisify`](https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original), for general purpose see [pify](https://www.npmjs.com/package/pify), [es6-promisify](https://www.npmjs.com/package/es6-promisify))
24622470

2463-
**Bad:**
2471+
回调不够简洁, 因为他们会产生过多的嵌套 *(回调地狱)* 。 这些工具可以将使用回调函数转换成返回 Promise 的函数 (对于 Node.js , 参考 [`util.promisify`](https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original) , 而对于更加通用的场景, 参考 [pify](https://www.npmjs.com/package/pify), [es6-promisify](https://www.npmjs.com/package/es6-promisify) )。
2472+
2473+
**不好的:**
24642474

24652475
```ts
24662476
import { get } from 'request';
@@ -2491,7 +2501,7 @@ downloadPage('https://en.wikipedia.org/wiki/Robert_Cecil_Martin', 'article.html'
24912501
});
24922502
```
24932503

2494-
**Good:**
2504+
**好的:**
24952505

24962506
```ts
24972507
import { get } from 'request';
@@ -2512,22 +2522,37 @@ downloadPage('https://en.wikipedia.org/wiki/Robert_Cecil_Martin', 'article.html'
25122522

25132523
Promises supports a few helper methods that help make code more conscise:
25142524

2525+
Promise 提供了一些帮助方法来让代码变得更加简洁:
2526+
25152527
| Pattern | Description |
25162528
| ------------------------ | ----------------------------------------- |
25172529
| `Promise.resolve(value)` | Convert a value into a resolved promise. |
25182530
| `Promise.reject(error)` | Convert an error into a rejected promise. |
25192531
| `Promise.all(promises)` |Returns a new promise which is fulfilled with an array of fulfillment values for the passed promises or rejects with the reason of the first promise that rejects. |
25202532
| `Promise.race(promises)`|Returns a new promise which is fulfilled/rejected with the result/error of the first settled promise from the array of passed promises. |
25212533

2534+
| Pattern | Description |
2535+
| ------------------------ | ----------------------------------------- |
2536+
| `Promise.resolve(value)` | 将一个值转换为一个已解决的 Promise 。 |
2537+
| `Promise.reject(error)` | 将一个错误转换为一个已拒绝的 Promise 。 |
2538+
| `Promise.all(promises)` | 从一组 Promise 返回一个新的 Promise , 如果这组 Promise 全部解决, 则解决新生成的 Promise , 否则拒绝新生成的 Promise 。 |
2539+
| `Promise.race(promises)`| 从多个 Promise 生成一个新的 Promise , 返回值由第一个解决或者拒绝的 Promise 决定。 |
2540+
25222541
`Promise.all` is especially useful when there is a need to run tasks in parallel. `Promise.race` makes it easier to implement things like timeouts for promises.
25232542

2524-
**[⬆ back to top](#table-of-contents)**
2543+
`Promise.all` 在需要并行运行任务时非常有用, 而 `Promise.race` 则可以比较容易的实现类似超时的 Promise 。
2544+
2545+
**[⬆ 返回目录](#目录)**
25252546

25262547
### Async/Await are even cleaner than Promises
25272548

2549+
### Async/Await 比 Promise 更加简洁
2550+
25282551
With `async`/`await` syntax you can write code that is far cleaner and more understandable that chained promises. Within a function prefixed with `async` keyword you have a way to tell the JavaScript runtime to pause the execution of code on the `await` keyword (when used on a promise).
25292552

2530-
**Bad:**
2553+
使用`async` /`await`语法,您可以编写更清晰,更易理解的链接承诺的代码。 通过在方法前面标记 `async` 关键字, 可以让 JavaScript 运行时在遇到 `async` 关键字时暂停(当使用 Promise 时才能这样做)。
2554+
2555+
**不好的:**
25312556

25322557
```ts
25332558
import { get } from 'request';
@@ -2569,7 +2594,7 @@ try {
25692594
}
25702595
```
25712596

2572-
**[back to top](#table-of-contents)**
2597+
**[返回目录](#目录)**
25732598

25742599
## Error Handling
25752600

0 commit comments

Comments
 (0)