簡體   English   中英

Promise / A + with chain then()回調用例?

[英]Promise/A+ with chain then() callbacks use case?

我剛剛開始閱讀有關Promise / A +的文章,並想親自嘗試一下。 我添加了多個then()回調,這種行為令人驚訝。

1)鏈接then不會返回相同的promise

  > new Promise(function(a, r) {a('hello')}).
      then(function(r) { console.log('1', arguments) }).
      then(function(r) { console.log("2", arguments) })
  1 ["hello"]
  2 [undefined]

2)非鏈接按預期工作

> p = new Promise(function(a, r) {a('hello')}); 
    p.then(function(r) { console.log('1', arguments) }); 
    p.then(function(r) { console.log("2", arguments) })
1 ["hello"]
2 ["hello"]

方案1的用例是什么?

您只應從承諾中回報價值。

 new Promise(function(a, r) {a('hello')}). then(function(r) { console.log('1', arguments); return r; }). then(function(r) { console.log("2", arguments) }) 

返回的值作為參數傳遞給then函數中的回調。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM