如何编写嵌套的Promises

时间:2016-11-17 09:52:06

标签: javascript ecmascript-6 es6-promise

考虑这个代码,其中startcontinuefinish是承诺。

export const do = () => {
    return new Promise((resolve, reject) => {
        start()
            .then(() => continue())
            .then(() => finish())
            .then(() => resolve())
            .catch((reason) => reject(reason))
    });
};

这是如何编写嵌套的promises?

1 个答案:

答案 0 :(得分:1)

只需返回整个链条,无需包裹它:

export const _do = () => start()
            .then(continue)
            .then(finish)
;