Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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

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)

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?

like image 799
Melissa Avatar asked Dec 22 '15 02:12

Melissa


People also ask

How does promise work in node JS?

A Promise in Node means an action which will either be completed or rejected. In case of completion, the promise is kept and otherwise, the promise is broken. So as the word suggests either the promise is kept or it is broken. And unlike callbacks, promises can be chained.

Why promise is used in JavaScript?

Promises are used to handle asynchronous operations in JavaScript. They are easy to manage when dealing with multiple asynchronous operations where callbacks can create callback hell leading to unmanageable code.

How do promises work JavaScript?

It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.

How do I resolve a returned promise?

The Promise. resolve() method "resolves" a given value to a Promise . If the value is a promise, that promise is returned; if the value is a thenable, Promise. resolve() will call the then() method with two callbacks it prepared; otherwise the returned promise will be fulfilled with the value.


2 Answers

Mozilla's javascript promises are based on ES6 standard, whereas jQuery promises were something created before ES6 was released.

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

There are actually a boatload of promise libraries, but to me the ES6 one is the simplest to understand. You don't need more than "then" and "catch" and it is real easy to chain together into a sequence of operations. Add to that with Promise.all for parallel tasks and 99% of what you need is covered.

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 things that jQuery does support that is not in ES6 is some sort of "progress" resolve.

like image 102
Steve Campbell Avatar answered Nov 14 '22 23:11

Steve Campbell


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.

How come the JS API on Mozilla doesn't have done()?

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.

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.

like image 21
Bergi Avatar answered Nov 14 '22 23:11

Bergi