Skip to content

Commit 2ea4295

Browse files
committed
调整欸容
Signed-off-by: meathill <meathill@gmail.com>
1 parent 1a63f96 commit 2ea4295

5 files changed

+47
-33
lines changed
File renamed without changes.

03-await-async.md

Whitespace-only changes.

04-01-downgrade.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
降级
2+
========
3+
4+
## IE
5+
6+
遗憾的是,除了 Edge 版本,IE 都不支持原生 Promise。不过好在 Promise 不需要新的语言元素,所以我们完全可以用独立类库来补足。这里推荐 [Q](https://github.com/kriskowal/q)[Bluebird](http://bluebirdjs.com/),它们都完全兼容 ES 规范,也就是说,前面介绍的用法都能奏效。
7+
8+
当然如果你不是非要 `new Promise()` 不可,用 jQuery 也完全可以。
9+
10+
## jQuery
11+
12+
jQuery 早在 1.5 版本就开始这方面的探索,已经实现了 Promise,不过名字不太一样,叫 [Deferred 对象](http://api.jquery.com/category/deferred-object/)。实现也不太一样,因为 jQuery 1.5 之后就开始这方面的尝试,所以和最终规范肯定有出入。不过升级到 [3.0](http://blog.meathill.com/tech/js/jquery/jquery-3-0-beta-released.html) 之后,它就完全遵守规范,并且也通过了测试。所以如果使用新版本,我们大可以按照之前的教程来操作,只是 jQuery 需要使用工厂方法来创建 Promise 实例,与规范略有区别:
13+
14+
```javascript
15+
$.deferred(function (resolve) {
16+
// 执行异步操作吧
17+
})
18+
.then( response => {
19+
// 继续下一步
20+
});
21+
```
22+
23+
另外,jQuery 的 [jqXHR](http://api.jquery.com/jQuery.ajax/#jqXHR) 对象也是 Promise 对象,所以完全可以用 `.then()` 方法操作:
24+
25+
```javascript
26+
$.ajax(url, {
27+
dataType: 'json'
28+
})
29+
.then(json => {
30+
// 做后续操作
31+
});
32+
```

04-upgrade.md 04-lets-practice.md

+11-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
改进老代码
1+
一起实战吧
22
========
33

44
学会新的异步操作之后,我们自然希望改造之前的异步回调代码。下面我就带领大家来试一试。
@@ -30,13 +30,22 @@ module.exports = {
3030
}
3131
};
3232

33-
// a.js
33+
// promise.js
3434
const fs = require('./FileSystem');
3535

3636
fs.readFile('../README.md', 'utf-8')
3737
.then(content => {
3838
console.log(content);
3939
});
40+
41+
// async.js
42+
const fs = require('.FileSystem');
43+
44+
async function read(path) {
45+
let content = await fs.readFile(path);
46+
console.log(content);
47+
}
48+
read();
4049
```
4150

4251
## 将任何异步操作都包装成 Promise
@@ -84,32 +93,3 @@ if (await openConfirmPopup('确定么')) {
8493
}
8594
```
8695

87-
## jQuery
88-
89-
jQuery 已经实现了 Promise,不过名字不太一样,叫 [Deferred 对象](http://api.jquery.com/category/deferred-object/)。实现也不太一样,因为 jQuery 1.5 之后就开始这方面的尝试,所以和最终规范肯定有出入。不过 [3.0](http://blog.meathill.com/tech/js/jquery/jquery-3-0-beta-released.html) 之后,它就完全遵守规范,并且也通过了测试。所以如果使用新版本,我们大可以按照之前的教程来操作,只是 jQuery 需要使用工厂方法来创建 Promise 实例,与规范略有区别:
90-
91-
```javascript
92-
$.deferred(function (resolve) {
93-
// 执行异步操作吧
94-
})
95-
.then( response => {
96-
// 继续下一步
97-
});
98-
```
99-
100-
另外,jQuery 的 [jqXHR](http://api.jquery.com/jQuery.ajax/#jqXHR) 对象也是 Promise 对象,所以完全可以用 `.then()` 方法操作:
101-
102-
```javascript
103-
$.ajax(url, {
104-
dataType: 'json'
105-
})
106-
.then(json => {
107-
// 做后续操作
108-
});
109-
```
110-
111-
## IE
112-
113-
遗憾的是,除了 Edge 版本,IE 都不支持原生的 Promise。但好在 Promise 不需要新的语言元素,所以我们完全可以用独立类库来补足。这里推荐 [Q](https://github.com/kriskowal/q)[Bluebird](http://bluebirdjs.com/),因为它们都完全兼容最新的规范,也就是可以完全使用之前介绍的方法。
114-
115-
当然如果你不是非要 `new Promise()` 不可,用 jQuery 也完全可以。

SUMMARY.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
* [Promise 方案](02-promise-intro.md)
66
* [Promise 入门](02-1-promise-basic.md)
77
* [Promise 进阶](02-2-promise-advanced.md)
8-
* [Await/Async 方案](03-await-async-.md)
9-
* [改进老代码](04-upgrade.md)
8+
* [Await/Async 方案](03-await-async.md)
9+
* [Await/Async 和 Promise 队列的异同](03-1-difference-between-await-async-and-promise.md)
10+
* [一起实战吧](04-lets-practice.md)
11+
* [降级](04-01-downgrade.md)
1012
* [回顾](10-review.md)
1113
* [Tips:小程序](20-1-xiaochengxu.md)

0 commit comments

Comments
 (0)