再看ES6的Promise by ety001

View this thread on steempeak.com
· @ety001 ·
$6.11
再看ES6的Promise
![](https://steemitimages.com/DQmW3it6ZFz89dGff96rVEGy3GWsMJgosJcx7967vHVYYnS/image.png)
(source [pixabay](https://pixabay.com/))

之前在写 [StellarBot](https://stellarbot.top) 的时候,就一直被 *Promise* 给困扰着,毕竟自己不是专业前端。这次准备解决 [StellarBot](https://stellarbot.top) 的 *BUG* 之前决定再次看看 *ES6* 的 *Promise*。

*Promise* 是一个对象,且有以下特点:

> 1. 对象只有三个状态:*pending*(进行中)、*fulfilled*(已成功)、*rejected*(已失败)。
> 2. 对象的状态不受外界影响,对象初始状态是 *pending*,终结状态是 *fulfilled* 或者 *rejected*。只有**异步操作**才能决定是从 *pending* 到 *fulfilled*,还是从 *pending* 到 *rejected*。
> 3. 一旦状态改变,就不会再变,任何时候都可以得到这个结果。

这次再看这些特点,终于找到了之前自己一直很晕的原因,就是忽略了对象只有一个状态,异步操作后也只有一个状态且不可变。

之前一直错误的以为 *Promise* 对象可以处理多个异步操作,靠的是链式的 *then()* 操作。

这个错误的思路把我带到沟里了很久。

**这次明白了,一个 *Promise* 只处理一个异步操作,如果在 *then()* 中还要异步操作,就要再创建一个 *Promise* 才行。**

明白了这个道理,再看 *Promise* 的各种示例代码,终于是看明白了,知道了这个流程到底是怎么走的了。

这下可以安心的去解决 *StellarBot* 的异步问题了。
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id34,931,727
authorety001
permlinkes6-promise
categorycn
json_metadata"{"app": "steemit/0.1", "format": "markdown", "links": ["https://pixabay.com/", "https://stellarbot.top"], "image": ["https://steemitimages.com/DQmW3it6ZFz89dGff96rVEGy3GWsMJgosJcx7967vHVYYnS/image.png"], "tags": ["cn", "cn-dev", "cn-programming"]}"
created2018-02-24 16:30:12
last_update2018-02-24 16:30:12
depth0
children6
net_rshares1,128,919,438,291
last_payout2018-03-03 16:30:12
cashout_time1969-12-31 23:59:59
total_payout_value4.946 SBD
curator_payout_value1.161 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length842
author_reputation192,999,220,769,295
root_title再看ES6的Promise
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (41)
@cn-naughty.boy ·
@ety001, 代码/编程/IT搞得很溜啊,陶醉本尊了,快扶我一下...
properties (22)
post_id34,931,892
authorcn-naughty.boy
permlink20180224t163105164z-post
categorycn
json_metadata"{"tags": ["cn"]}"
created2018-02-24 16:31:06
last_update2018-02-24 16:31:06
depth1
children0
net_rshares0
last_payout2018-03-03 16:31:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length37
author_reputation804,554,661,723
root_title再看ES6的Promise
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@cnbuddy ·
新年快乐!cn区点赞机器人 @cnbuddy 这厢有礼了。倘若你想让我隐形,请回复“取消”。
properties (22)
post_id34,935,971
authorcnbuddy
permlinkre-ety001-es6-promise-20180224t165554561z
categorycn
json_metadata{}
created2018-02-24 16:55:54
last_update2018-02-24 16:55:54
depth1
children0
net_rshares0
last_payout2018-03-03 16:55:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length46
author_reputation-1,405,328,253,928
root_title再看ES6的Promise
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@justyy ·
promise 可以串起来吧, 我记得是 大概是 f().then().then()...catch()
properties (22)
post_id35,194,567
authorjustyy
permlinkre-ety001-es6-promise-20180225t220316878z
categorycn
json_metadata"{"app": "steemit/0.1", "tags": ["cn"]}"
created2018-02-25 22:03:18
last_update2018-02-25 22:03:18
depth1
children1
net_rshares0
last_payout2018-03-04 22:03:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length52
author_reputation2,089,296,130,854,040
root_title再看ES6的Promise
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@ety001 ·
我之前对于连续的then()并不理解。尤其是使用 stellar 官方封装的 js 库,在没有接触过 Promise 的情况下,使用 Promise 完成异步中再异步,就把我给弄晕了。

现在明白了,如果在一次异步完成后再进行异步,第二次异步需要重新new一个Promise对象出来并返回。之前不明白,是因为 Stellar 的 JS 库把这个过程封装进去了。

不知道我说明白了。。。😂
properties (22)
post_id35,257,337
authorety001
permlinkre-justyy-re-ety001-es6-promise-20180226t053922682z
categorycn
json_metadata"{"app": "steemit/0.1", "tags": ["cn"]}"
created2018-02-26 05:39:24
last_update2018-02-26 05:39:24
depth2
children0
net_rshares0
last_payout2018-03-05 05:39:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length194
author_reputation192,999,220,769,295
root_title再看ES6的Promise
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@shrewdmark ·
关注你啦,感觉 Promise  确实是javascript的一个难点呢
properties (22)
post_id35,715,768
authorshrewdmark
permlinkre-ety001-es6-promise-20180228t061014512z
categorycn
json_metadata"{"app": "steemit/0.1", "tags": ["cn"]}"
created2018-02-28 06:10:15
last_update2018-02-28 06:10:15
depth1
children1
net_rshares0
last_payout2018-03-07 06:10:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length36
author_reputation0
root_title再看ES6的Promise
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@ety001 ·
多谢关注。Promise我也是看了好久。
properties (22)
post_id35,728,112
authorety001
permlinkre-shrewdmark-re-ety001-es6-promise-20180228t073146425z
categorycn
json_metadata"{"app": "steemit/0.1", "tags": ["cn"]}"
created2018-02-28 07:31:48
last_update2018-02-28 07:31:48
depth2
children0
net_rshares0
last_payout2018-03-07 07:31:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length20
author_reputation192,999,220,769,295
root_title再看ES6的Promise
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000