承诺不只是回调吗?

编程入门 行业动态 更新时间:2024-10-26 10:40:07
本文介绍了承诺不只是回调吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经开发 JavaScript 几年了,我完全不理解关于 Promise 的大惊小怪.

I've been developing JavaScript for a few years and I don't understand the fuss about promises at all.

看来我所做的只是改变:

It seems like all I do is change:

api(function(result){ api2(function(result2){ api3(function(result3){ // do work }); }); });

无论如何我都可以使用像 async 这样的库,例如:

Which I could use a library like async for anyway, with something like:

api().then(function(result){ api2().then(function(result2){ api3().then(function(result3){ // do work }); }); });

哪个代码更多,可读性更低.我在这里没有任何收获,它也不是突然神奇地平坦".更不用说必须将事情转换为承诺.

Which is more code and less readable. I didn't gain anything here, it's not suddenly magically 'flat' either. Not to mention having to convert things to promises.

那么,这里的承诺有什么大惊小怪的?

So, what's the big fuss about promises here?

推荐答案

Promise 不是回调.承诺代表异步操作的未来结果.当然,按照你的方式编写它们,你得到的好处很少.但是如果你按照它们的用途来编写它们,你可以用一种类似于同步代码的方式来编写异步代码,并且更容易理解:

Promises are not callbacks. A promise represents the future result of an asynchronous operation. Of course, writing them the way you do, you get little benefit. But if you write them the way they are meant to be used, you can write asynchronous code in a way that resembles synchronous code and is much more easy to follow:

api().then(function(result){ return api2(); }).then(function(result2){ return api3(); }).then(function(result3){ // do work });

当然,代码不会少很多,但可读性要高得多.

Certainly, not much less code, but much more readable.

但这不是结束.让我们发现真正的好处:如果您想检查任何步骤中的任何错误怎么办?用回调来做这件事会很糟糕,但用 promise 是小菜一碟:

But this is not the end. Let's discover the true benefits: What if you wanted to check for any error in any of the steps? It would be hell to do it with callbacks, but with promises, is a piece of cake:

api().then(function(result){ return api2(); }).then(function(result2){ return api3(); }).then(function(result3){ // do work }).catch(function(error) { //handle any error that may occur before this point });

与 try { ... } catch 块几乎相同.

更好:

api().then(function(result){ return api2(); }).then(function(result2){ return api3(); }).then(function(result3){ // do work }).catch(function(error) { //handle any error that may occur before this point }).then(function() { //do something whether there was an error or not //like hiding an spinner if you were performing an AJAX request. });

甚至更好:如果这 3 个对 api、api2、api3 的调用可以同时运行(例如,如果它们是 AJAX 调用)怎么办?但你需要等三个?如果没有承诺,您应该必须创建某种计数器.有了 Promise,使用 ES6 表示法,又是小菜一碟,而且非常简洁:

And even better: What if those 3 calls to api, api2, api3 could run simultaneously (e.g. if they were AJAX calls) but you needed to wait for the three? Without promises, you should have to create some sort of counter. With promises, using the ES6 notation, is another piece of cake and pretty neat:

Promise.all([api(), api2(), api3()]).then(function(result) { //do work. result is an array contains the values of the three fulfilled promises. }).catch(function(error) { //handle the error. At least one of the promises rejected. });

希望你现在以全新的眼光看待 Promise.

Hope you see Promises in a new light now.

更多推荐

承诺不只是回调吗?

本文发布于:2023-10-12 21:02:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485886.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!