4

I recently downloaded a library that uses ES6 Promises. Since I want to deploy to browsers which don't support Promises I also downloaded a polyfill.

Since I've got jQuery included anyway I thought about writing a polyfill for Promise which internally uses jQuery's Deferred.

I wrote this simple polyfill which is enough for my specific use case:

    window.Promise = function(cb){
        var promise = $.Deferred();

        cb(promise.resolve, promise.reject);

        return promise.promise();
    };

The problem with this is that it doesn't cover the whole specification (thinks like Promise.all() are missing).

Before I invest a lot of time into this I'd like to know if it is possible to write a full polyfill for Promise using jQuery's Deferred. Or are there some features which can't be replicated?

Thomas
  • 8,426
  • 1
  • 25
  • 49
  • 1
    I remember someone saying that jquery's deferred don't stick to the promise specs very well, so it might be better to use something else. – simonzack Aug 31 '14 at 14:09
  • I heard that too but I seem to remember it was more about how jQuery's functions use promises and not about the Deferred in general. But I might be wrong. – Thomas Aug 31 '14 at 14:24
  • 1
    Personally, I would use the polyfill you already have, and keep jQuery Deferreds/Promises well and truly separate unless you are unavoidably cornered into mixing them. – Roamer-1888 Aug 31 '14 at 15:28
  • @Thomas Could compose piece without utilizing jquery , including implementation for different usages ; a `when` , `done` , `callback` , etc . Perhaps create flow graph of prospective implementation , with documentation of project implementation goals. A brief piece utilizing js without jquery , which includes a `when` and `done` ; `.then` is native promise , chain-able http://stackoverflow.com/a/23587868 . If possible , can describe which processing features which would be provided ?, that `promise` , jquery `deferred` implementation not provide ? Thanks – guest271314 Sep 02 '14 at 05:12

1 Answers1

3

things like Promise.all() are missing

Promise.all can be more or less replicated by using $.when. Promise.race can be replicated by creating a deferred whose resolve/reject methods are attached to all input promises.

The problem with this is that it doesn't cover the whole specification

No. The parts that are not covered could easily be added. The real problem is that the existing parts of the jQuery Deferred implement do not comply with the specification - see Problems inherent to jQuery $.Deferred (jQuery 1.x/2.x)

Before I invest a lot of time into this I'd like to know if it is possible to write a full polyfill for Promise using jQuery's Deferred. Or are there some features which can't be replicated?

Everything can be replicated, but you'd need to monkeypatch enough in the Deferred implementation that you are better of just using one of the existing polyfills. If you really want to create your own, you might base it on a jQuery.Callbacks("once memory").

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375