James Newton
James Newton

Reputation: 7082

Replace $.Deferred with plain JavaScript Promise

I am working on a Chrome extension which has no reason to use jQuery, but which does have a reason to use the meteor-ddp.js script, which uses jQuery only in order to access the $.Deferred functionality. It only uses three methods: reject, resolve and promise:

var conn = new $.Deferred();
//...
conn.reject(err);
//...
conn.resolve(data);
//...
return conn.promise();

Now that JavaScript has its own native Promise objects, it seems unnecessary to include at least 69 KB of jQuery slim.min.js just to provide a promise feature.

It would be great if someone with more experience than I have of jQuery and promises could explain how the $.Deferred functionality works, and how it could be replaced with native Promises.

Upvotes: 5

Views: 2283

Answers (1)

Alex
Alex

Reputation: 1722

This would be the equivalent of your code sample in es6:

return new Promise((resolve, reject) => {
  // ...
  reject(err);
  // ...
  resolve(data);
});

Upvotes: 8

Related Questions