Why is the jQuery promise fulfilled (), but the Javascript promise documented by Mozilla isnโ€™t it? What if I want to do done () in JS?

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

Promise Mozilla, it seems, only 2 methods: then catch. It seems that there are more methods in jQuery, including: then, done and not executed. ( from here )

Why doesn't the JS API in Mozilla have done() ? What if I want to have executed () functionality in JavaScript? What should I do?

+5
source share
3 answers

Mozilla javascript promises are based on the ES6 standard , while jQuery promises was created before ES6.

Based on my reading of jQuery docs, ES6 then equivalent to jQuery done .

Actually there is a boat for the libraries with promises, but for me ES6 is one the easiest to understand. You do not need more than "then" and "catch", and it is very easy to combine them into a sequence of operations. Add to this with Promise.all for parallel tasks and take advantage of 99% of what you need.

 return doSomething().then(function(result) { return doSomethingElse(result); }).then(function(secondResult) { return doThirdSomething(secondResult); }).catch(function(err) { console.log(err); }).then(function(finalResult) { // a then after a catch is like a "finally" or "always" return finalResult; }); 

Some of the things jQuery supports, not ES6, is a kind of "progress."

+3
source

The jQuery deferred API is bloated and preceded by promise libraries. As soon as they realized how useful promises were, they added the then method (or previosly, pipe ), but they were not able to get 100% rights .

Why doesn't the JS API in Mozilla have done() ?

This is completely unnecessary. All Promises / A + compatible implementations (including ES6) need only one method: .then() . It is completely universal, you can do everything with it - these are primitive promises.

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

Well, you can 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 this, this is not very useful in fact. It does not catch and ignores exceptions, so you just need to use then everywhere.

+3
source

I can say why ES6 doesn't have .done . However, you can still have something similar to .done .

 yourPromiseObject.then(successHandler).catch(failureHandler).then(doneHandler); 

The first then handles the success response, but will not be called using the yourPromiseObject function yourPromiseObject reject or throw an exception. The second catch is crucial in โ€œresolvingโ€ any exceptions / rejections (in short, you say that the promise object continues to go, you know that there is an error). Make sure that you return something really, even that which is simple as failureHandler consists of nothing but return 0 . The final .then should now be called, so your .done

0
source

Source: https://habr.com/ru/post/1238907/


All Articles