为什么jQuery的承诺有一个done(),但是Mozilla记录的Javascript承诺不是?如果我想在JS中有一个done()怎么办? [英] Why does jQuery's promise have a done(), but Javascript's promise documented by Mozilla does not? What if I want to have a done() in JS?

查看:111
本文介绍了为什么jQuery的承诺有一个done(),但是Mozilla记录的Javascript承诺不是?如果我想在JS中有一个done()怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mozilla的JavaScript文档'Promises之间有什么区别(参见 API页面)和jQuery的Promises(参见 API页面)?

What are the differences between Mozilla's JavaScript docs' Promises (see API page) and jQuery's Promises (see API page)?

Mozilla的承诺似乎只有两种方法:然后赶上。 jQuery的承诺似乎有更多的方法,包括:then,done和fail。 (从这里开始

Mozilla's promise seems to have only 2 methods: then and catch. jQuery's promise seems to have more methods, including: then, done, and fail. (from here)

为什么Mozilla上的JS API没有 done()?如果我想在JavaScript中使用done()功能怎么办?我该怎么办?

How come the JS API on Mozilla doesn't have done()? What if I want to have a done() functionality in JavaScript? What do I do?

推荐答案

jQuery的延迟API 是膨胀的,早于承诺库。一旦他们意识到承诺有多么有用,他们就添加了然后(或以前, pipe )方法,但是他们未能100%正确

jQuery's deferred API is bloated and predates promise libraries. Once they realised how useful promises were, they added a then (or previosly, pipe) method, however it they failed to get it 100% right.


为什么Mozilla上的JS API没有 done()

完全没必要。所有 Promises / A +兼容实施(包括ES6)只需要一种方法:。然后()。它完全是通用的,你可以用它做任何事情 - 这是承诺的原始

It's completely unnecessary. All Promises/A+ compatible implementations (which includes ES6) only need a single method: .then(). It's completely universal, you can do everything with it - it's the primitive of promises.


如果我想在JavaScript中使用 done()功能怎么办?我该怎么办?

What if I want to have a done() functionality in JavaScript? What do I do?

好吧,你可以自己实现它:

Well, you could implement it yourself:

Promise.prototype.done = function(cb) { // or function(...cbs) for (let cb of cbs) …
    this.then(cb).then(null, function(err) { /* ignore */ });
    return this;
};

但是从中可以看出,它实际上并不是很有用。它没有链,它忽略了异常,所以你应该只使用然后无处不在。

But as you can see from that, it's not very useful actually. It doesn't chain, and it ignores exceptions, so you should just use then everywhere.

这篇关于为什么jQuery的承诺有一个done(),但是Mozilla记录的Javascript承诺不是?如果我想在JS中有一个done()怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆