推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
nyse

JS: 在 then 里面又调用了一个返回 Promise 的函数,是不是不太优雅?

  •  
  •   nyse · Sep 26, 2018 · 7956 views
    This topic created in 2961 days ago, the information mentioned may be changed or developed.

    大概就像下面情况:

    someFunc()
    .then(() => {
        somePromiseFunc()
        .then(() => {
        
        })
        .catch(() => {
    
        })
    })
    .catch(() => {
    
    })
    

    Promise 本来就是用来解决嵌套回调不清晰的问题,但是现在又遇到这种问题,如果 Promise 成功后,又调用一个返回 Promise 的函数,还是会出现这种嵌套的问题。

    Supplement 1  ·  Sep 26, 2018
    好的,听了大家的意见,用 async/await 改写了。

    还想问问各位大佬,ES 变化这么快,你们是怎么保持跟进学习的?
    23 replies  •  2018-10-10 21:21:41 +08:00
    dilu
    dilu
    Sep 26, 2018
    1
    业务本身就是逻辑套逻辑 不管怎么样总归都会有这种情况出现

    promise 不是把回调干掉了,而是换了一种写法,更加的简介而已

    回调噩梦一直会存在,还好我是做后端的,哈哈哈,逃。
    p1llar
    p1llar
    Sep 26, 2018❤️2
    2
    可以写成:

    ```javascript
    someFunc()
    .then(() => {
    return somePromiseFunc()
    })
    .then(() => {

    })
    .catch(() => {
    })
    ```

    或者直接用 async/await 吧,Promise 还是太丑了
    cuzfinal
    cuzfinal
    Sep 26, 2018
    3
    用 async/await
    CDL
    CDL
    Sep 26, 2018
    4
    还好楼上是做后端的,手动狗头
    多看文档和教程,return promise 是可以继续 then 链式调用的
    Jeepeng
    Jeepeng
    Sep 26, 2018❤️1
    5
    可以用 async/await 改写
    ```js
    try {
    const result = await someFunc();
    await somePromiseFunc();
    } catch(err) {}
    ```
    icris
    icris
    Sep 26, 2018
    6
    someFunc()
    .then(somePromiseFunc)
    .catch(e => {

    })
    icanfork
    icanfork
    Sep 26, 2018
    7
    链式调用
    shakaraka
    shakaraka
    Sep 26, 2018
    8
    在 then 里 return 一个 promise,可以直接继续 then,或则 async/await
    whypool
    whypool
    Sep 26, 2018
    9
    async/await 写起来才好看
    AV1
    AV1
    Sep 26, 2018 via Android
    10
    除了封装接口的需要,不然都全用 async 函数。
    q397064399
    q397064399
    Sep 26, 2018
    11
    async/await 只能这样了
    lhx2008
    lhx2008
    Sep 26, 2018 via Android
    12
    @dilu 后端也可以写回调,只要是异步框架
    Exin
    Exin
    Sep 26, 2018
    13
    如果异步操作需要并行的话,可以取楼主本来的写法,但这种做法比较极端、不常见。
    kuaner
    kuaner
    Sep 26, 2018
    14
    链式调用,直接 return 内部那个 promise 就好了吧

    不要写成地狱的形式....
    liuhuansir
    liuhuansir
    Sep 26, 2018
    15
    链式调用比 async/await 优雅吧,很有条理性,类似的 rx 系列有各个语言的实现版
    shijianit
    shijianit
    Sep 26, 2018
    16
    promise 本身就是为了避免层层嵌套
    per
    per
    Sep 26, 2018 via iPhone
    17
    Promise.all?
    dilu
    dilu
    Sep 26, 2018 via Android
    18
    @lhx2008 不好意思我是拍黄片的哈哈哈,我们基本没啥机会写异步,哈哈哈(ಡωಡ)hiahiahia
    wsxyeah
    wsxyeah
    Sep 26, 2018
    19
    Promise 能够写成一条链是理想化的情况了。实际用的时候会发现嵌套难以避免。
    很多地方的数据流并不是理想化的管道传递,而是需要透传数据给后面的情况,这时候就不得不嵌套;另外一种情况是需要 catch 局部错误而不想影响整条链的错误传递。
    wizardforcel
    wizardforcel
    Sep 26, 2018 via Android
    20
    只有我一个人想到 return somePromiseFunc()嘛。。
    FakeLeung
    FakeLeung
    Sep 26, 2018
    21
    someFunc().then(somePromiseFunc).catch()

    or

    try{
    const res1 = await someFunc()
    const res2 = await somePromiseFunc(res1)
    } catch(e) {}
    whosesmile
    whosesmile
    Sep 27, 2018
    22
    除非写 node, 纯前端代码,我习惯还是写 promise;
    asyc 的问题是异常处需要 try catch,这让我很蛋疼
    civet
    civet
    Oct 10, 2018 via iPhone
    23
    不是二楼已经终结了问题吗?同意楼上,前端用 promise 就够了。如果还需要三四次或更多的异步操作,难道不是服务 API 设计有问题?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1289 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 22:16 · PVG 06:16 · LAX 14:16 · JFK 17:16
    ♥ Do have faith in what you're doing.