ES6的承诺相当于jQuery的“始终”是什么?(复制)

时间:2022-08-23 11:45:06

This question already has an answer here:

这个问题已经有了答案:

I have something like the following:

我有以下几点:

getUser("foo").then(handleSuccess, handleError).always(tidyUp);

getUser returns a jQuery Deferred object.

getUser返回一个jQuery延迟对象。

I understand from this article that I can convert the Deferred object to a native Promise using Promise.resolve, so I can write

我从本文中了解到,我可以使用Promise将Deferred对象转换为native Promise。下定决心,这样我就可以写作了

Promise.resolve(getUser("foo"))
  .then(handleSuccess)
  .catch(handleError)

The Promise API doesn't offer an always method though, so I'm wondering how that should be handled.

Promise API并没有提供一个总是方法,所以我想知道应该如何处理这个问题。

Is it as follows?

是如下吗?

 Promise.resolve(getUser("foo"))
  .then(handleSuccess)
  .then(tidyUp)
  .catch(handleError)
  .then(tidyUp)

2 个解决方案

#1


19  

I think the following is what you're looking for:

我想以下就是你要找的:

 Promise.resolve(getUser("foo"))
  .then(handleSuccess, handleError)
  .then(tidyUp)

tidyUp will be always called. See the following jsbin for the full example: http://jsbin.com/lujubu/edit?html,js,console,output

tidyUp总是被称为tidyUp。完整示例请参见下面的jsbin: http://jsbin.com/lujubu/edit?html、js、控制台、输出

#2


3  

Use your always function as the handler for resolve and reject to ensure it will always be called.

使用您的always函数作为解决和拒绝的处理程序,以确保它始终被调用。

function getUser(result) {
    switch (result) {
        case 'good':
            return Promise.resolve();

        case 'bad':
            return Promise.reject();

        case 'ugly':
            return new Promise(() => { throw new Error() })
    }
}

function handleSuccess() { console.log('success') }
function handleError() { console.log('error') }
function tidyUp() { console.log('all tidy now') }


Promise.resolve(getUser('good'))
    .then(handleSuccess)
    .catch(handleError)
    .then(tidyUp, tidyUp);

Promise.resolve(getUser('bad'))
    .then(handleSuccess)
    .catch(handleError)
    .then(tidyUp, tidyUp);

Promise.resolve(getUser('ugly'))
    .then(handleSuccess)
    .catch(handleError)
    .then(tidyUp, tidyUp);

// success
// error
// error
// all tidy now
// all tidy now
// all tidy now

Promise API Reference

承诺API参考

#1


19  

I think the following is what you're looking for:

我想以下就是你要找的:

 Promise.resolve(getUser("foo"))
  .then(handleSuccess, handleError)
  .then(tidyUp)

tidyUp will be always called. See the following jsbin for the full example: http://jsbin.com/lujubu/edit?html,js,console,output

tidyUp总是被称为tidyUp。完整示例请参见下面的jsbin: http://jsbin.com/lujubu/edit?html、js、控制台、输出

#2


3  

Use your always function as the handler for resolve and reject to ensure it will always be called.

使用您的always函数作为解决和拒绝的处理程序,以确保它始终被调用。

function getUser(result) {
    switch (result) {
        case 'good':
            return Promise.resolve();

        case 'bad':
            return Promise.reject();

        case 'ugly':
            return new Promise(() => { throw new Error() })
    }
}

function handleSuccess() { console.log('success') }
function handleError() { console.log('error') }
function tidyUp() { console.log('all tidy now') }


Promise.resolve(getUser('good'))
    .then(handleSuccess)
    .catch(handleError)
    .then(tidyUp, tidyUp);

Promise.resolve(getUser('bad'))
    .then(handleSuccess)
    .catch(handleError)
    .then(tidyUp, tidyUp);

Promise.resolve(getUser('ugly'))
    .then(handleSuccess)
    .catch(handleError)
    .then(tidyUp, tidyUp);

// success
// error
// error
// all tidy now
// all tidy now
// all tidy now

Promise API Reference

承诺API参考