![](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* 的异步问题了。
post_id | 34,931,727 |
---|---|
author | ety001 |
permlink | es6-promise |
category | cn |
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"]}" |
created | 2018-02-24 16:30:12 |
last_update | 2018-02-24 16:30:12 |
depth | 0 |
children | 6 |
net_rshares | 1,128,919,438,291 |
last_payout | 2018-03-03 16:30:12 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 4.946 SBD |
curator_payout_value | 1.161 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 842 |
author_reputation | 192,999,220,769,295 |
root_title | 再看ES6的Promise |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
abit | 0 | 433,928,780,846 | 16% | ||
lemooljiang | 0 | 447,703,741,274 | 100% | ||
justyy | 0 | 158,229,719,733 | 21.26% | ||
happyukgo | 0 | 184,977,787 | 10% | ||
nationalpark | 0 | 5,788,278,134 | 100% | ||
ety001 | 0 | 1,662,707,159 | 100% | ||
rainyapril | 0 | 52,444,520 | 10% | ||
shengjian | 0 | 1,915,003,075 | 10% | ||
geass | 0 | 52,317,477 | 10% | ||
tvb | 0 | 13,426,931,180 | 30% | ||
superbing | 0 | 870,237,579 | 10% | ||
dailyfortune | 0 | 610,674,255 | 9% | ||
xiaoshancun | 0 | 463,933,004 | 100% | ||
dailystats | 0 | 533,209,176 | 4% | ||
brendashockley | 0 | 70,509,084 | 25% | ||
dennisphillips | 0 | 73,574,697 | 25% | ||
ivysrono | 0 | 109,121,027 | 10% | ||
cnbuddy | 0 | 8,860,682,018 | 1% | ||
anxin | 0 | 50,969,237 | 10% | ||
cnsteem | 0 | 903,823,945 | 1% | ||
blockd | 0 | 52,179,125 | 10% | ||
moobear | 0 | 168,098,600 | 10% | ||
prch | 0 | 88,376,376 | 10% | ||
nean | 0 | 52,457,882 | 10% | ||
liuye | 0 | 50,331,199,467 | 100% | ||
foodielifestyle | 0 | 146,278,043 | 10% | ||
woolfe19861008 | 0 | 78,805,455 | 10% | ||
benytg | 0 | 408,991,478 | 100% | ||
dailychina | 0 | 888,116,369 | 10% | ||
vincenthan | 0 | 52,250,802 | 10% | ||
serenazz | 0 | 79,850,157 | 10% | ||
cha0s0000 | 0 | 99,895,412 | 3% | ||
zasilla | 0 | 52,425,573 | 10% | ||
cn-naughty.boy | 0 | 208,899,253 | 1.13% | ||
deepthinking | 0 | 52,242,183 | 10% | ||
nationalss | 0 | 150,573,283 | 100% | ||
lilypang22 | 0 | 183,100,520 | 10% | ||
steemitvip | 0 | 52,110,432 | 10% | ||
lakemountain | 0 | 229,725,320 | 10% | ||
regals | 0 | 52,227,354 | 10% | ||
ldsn001 | 0 | 0 | 50% |
@ety001, 代码/编程/IT搞得很溜啊,陶醉本尊了,快扶我一下...
post_id | 34,931,892 |
---|---|
author | cn-naughty.boy |
permlink | 20180224t163105164z-post |
category | cn |
json_metadata | "{"tags": ["cn"]}" |
created | 2018-02-24 16:31:06 |
last_update | 2018-02-24 16:31:06 |
depth | 1 |
children | 0 |
net_rshares | 0 |
last_payout | 2018-03-03 16:31:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 37 |
author_reputation | 804,554,661,723 |
root_title | 再看ES6的Promise |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
新年快乐!cn区点赞机器人 @cnbuddy 这厢有礼了。倘若你想让我隐形,请回复“取消”。
post_id | 34,935,971 |
---|---|
author | cnbuddy |
permlink | re-ety001-es6-promise-20180224t165554561z |
category | cn |
json_metadata | {} |
created | 2018-02-24 16:55:54 |
last_update | 2018-02-24 16:55:54 |
depth | 1 |
children | 0 |
net_rshares | 0 |
last_payout | 2018-03-03 16:55:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 46 |
author_reputation | -1,405,328,253,928 |
root_title | 再看ES6的Promise |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
promise 可以串起来吧, 我记得是 大概是 f().then().then()...catch()
post_id | 35,194,567 |
---|---|
author | justyy |
permlink | re-ety001-es6-promise-20180225t220316878z |
category | cn |
json_metadata | "{"app": "steemit/0.1", "tags": ["cn"]}" |
created | 2018-02-25 22:03:18 |
last_update | 2018-02-25 22:03:18 |
depth | 1 |
children | 1 |
net_rshares | 0 |
last_payout | 2018-03-04 22:03:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 52 |
author_reputation | 2,089,296,130,854,040 |
root_title | 再看ES6的Promise |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
我之前对于连续的then()并不理解。尤其是使用 stellar 官方封装的 js 库,在没有接触过 Promise 的情况下,使用 Promise 完成异步中再异步,就把我给弄晕了。 现在明白了,如果在一次异步完成后再进行异步,第二次异步需要重新new一个Promise对象出来并返回。之前不明白,是因为 Stellar 的 JS 库把这个过程封装进去了。 不知道我说明白了。。。😂
post_id | 35,257,337 |
---|---|
author | ety001 |
permlink | re-justyy-re-ety001-es6-promise-20180226t053922682z |
category | cn |
json_metadata | "{"app": "steemit/0.1", "tags": ["cn"]}" |
created | 2018-02-26 05:39:24 |
last_update | 2018-02-26 05:39:24 |
depth | 2 |
children | 0 |
net_rshares | 0 |
last_payout | 2018-03-05 05:39:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 194 |
author_reputation | 192,999,220,769,295 |
root_title | 再看ES6的Promise |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
关注你啦,感觉 Promise 确实是javascript的一个难点呢
post_id | 35,715,768 |
---|---|
author | shrewdmark |
permlink | re-ety001-es6-promise-20180228t061014512z |
category | cn |
json_metadata | "{"app": "steemit/0.1", "tags": ["cn"]}" |
created | 2018-02-28 06:10:15 |
last_update | 2018-02-28 06:10:15 |
depth | 1 |
children | 1 |
net_rshares | 0 |
last_payout | 2018-03-07 06:10:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 36 |
author_reputation | 0 |
root_title | 再看ES6的Promise |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
多谢关注。Promise我也是看了好久。
post_id | 35,728,112 |
---|---|
author | ety001 |
permlink | re-shrewdmark-re-ety001-es6-promise-20180228t073146425z |
category | cn |
json_metadata | "{"app": "steemit/0.1", "tags": ["cn"]}" |
created | 2018-02-28 07:31:48 |
last_update | 2018-02-28 07:31:48 |
depth | 2 |
children | 0 |
net_rshares | 0 |
last_payout | 2018-03-07 07:31:48 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 20 |
author_reputation | 192,999,220,769,295 |
root_title | 再看ES6的Promise |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |