简体   繁体   English

如何用then / catch“最终”承诺?

[英]How to “finally” on promise with then/catch?

How to do a proper finally on a promise? 最终如何正确地履行诺言?

Can I add a finally equivalent to this? 我可以添加一个最终等同于此的东西吗?

functionReturningPromise()
   .then(success => {})
   .catch(error => {})

or should I do it this way (does this even work as expected)? 还是我应该这样做(这是否按预期工作)?

functionReturningPromise()
   .then(success => {},error => {}).then(() => { /* finally code */ })

You can use 您可以使用

.finally()

Example: 例:

functionReturningPromise()
.then(data=>console.log(data))
.catch(error=>console.log(error))
.finally(()=>console.log("FINISH"))

The finally is called allways 最后被称为反正

You can do this in two ways: 您可以通过两种方式执行此操作:

  1. Use finally from Promise.prototype.finally() 最后使用Promise.prototype.finally()
yourPromiseFunction
  .then(function(json) { /* process your JSON further */ })
  .catch(function(error) { console.log(error); /* this line can also throw, e.g. when console = {} */ })
  .finally(function() { console.log("finally") });
  1. Or you can add an additional then() after the catch() to do finally in all the browsers. 或者,您可以在catch()之后添加一个额外的then()以最终在所有浏览器中执行。

Example: 例:

yourPromiseFunction.then(() => console.log('Resolved'))
            .catch(() => console.log('Failed'))
            .then(() => console.log('This is your finally'));

Yes, finally works with promises. 是的,终于可以兑现承诺了。

Example: 例:

let isLoading = true;

fetch(myRequest).then(function(response) {
        var contentType = response.headers.get("content-type");
        if (contentType && contentType.includes("application/json")) {
            return response.json();
        }
        throw new TypeError("Oops, we haven't got JSON!");
    })
    .then(function(json) {
        /* process your JSON further */
    })
    .catch(function(error) {
        console.log(error); /* this line can also throw, e.g. when console = {} */
    })
    .finally(function() {
        isLoading = false;
    });

For further reference refer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally 有关更多参考,请参阅https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Promise `.then`、`.catch` 和 `.finally` 如何以及何时进入 EventLoop 微任务队列? - How and when Promise `.then`, `.catch` and `.finally` land in EventLoop micro-tasks queue? 最后如何执行另一个promise? - How to execute another promise in finally? 在其他地方处理尝试/捕获的 promise 上调用.finally 会导致 nodejs 中的 UnhandledPromiseRejection - Calling .finally on a promise that is try/catch handled elsewhere causes UnhandledPromiseRejection in nodejs 如何将带有最终错误的Q Promise链接到finally() - How to chain Q promise with previous errors on finally() 如何在最终使用 JEST 运行 promise 之前对其进行测试 - How to test promise before it runs finally with JEST 如何使用catch,最后使用when.map - How to use catch and finally with when.map 我可以在 Promise.all() 中同时使用 multiple.finally() 和 multiple.catch() 吗? - Can I have multiple .finally() as well as multiple .catch() in Promise.all()? 从 promise 语法转换为 jQuery ajax ZC1C425268E68385D1AB5074C17AlyF 和tryFinal 425268E68385D1AB5074C17AlyF4 失败 - Failed conversion from promise syntax for jQuery ajax function to try/catch/finally and async/await? 如何.catch Promise.reject - How to .catch a Promise.reject 如何在Promise中捕获未捕获的异常 - How to catch uncaught exception in Promise
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM