Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide different Promises implementation, Q.js, bluebird, native Promise #17

Open
marcog83 opened this issue Oct 19, 2014 · 3 comments

Comments

@marcog83
Copy link

Hi,
I love this library and I used it a lot. It became my default ajax request library.
for example here http://www.pirelli.com/tyres/it-it/auto/home
I think this is not an issue, but is there any plan to let developer which Promise implementation to choose? I mean a sort of an additional parameter that stores Q.js or bluebird and then the returned promise would be the kind the develper have choosen.

best
marco

@geowa4
Copy link
Owner

geowa4 commented Oct 20, 2014

I would love to do that. The only problem I have is handling the UMD block listed below, specifically the AMD portion. If you know how to try Q and then fall back to another option, I'll gladly add this feature.

if (typeof module !== 'undefined' && typeof module.exports === 'object') module.exports = factory(require('q'))
else if (typeof define === 'function' && define.amd) define(['q'], factory)
else this['pj'] = factory(this['Q'])

@marcog83
Copy link
Author

As far as the definition of Promise implementation, I moved out of the library dipendency.
for example in myscript.js I required both pajamas and bluebird

var pj = require("pajamas"), bluebird=require("bluebird");

then I did

pj.Promise=bluebird;

In pajamas library I refactored a bit the function, in order to delete deferred Object

image

and this point
image

Not totally sure this is an elegant solution, but it could be a starting point to figure out something better.
I tried 3 different implementations and they seem to work

pj.Promise= window.Promise;
pj.Promise= Q.Promise;
pj.Promise= bluebird;

@geowa4
Copy link
Owner

geowa4 commented Oct 21, 2014

Looking at the UMD block, this is easy to solve in two of the three cases.

if (typeof module !== 'undefined' && typeof module.exports === 'object') module.exports = factory(require('q'))
else if (typeof define === 'function' && define.amd) define(['q'], factory)
else this['pj'] = factory(this['Q'])

The first can be solved with a little function:

function loadFirst (modules) {
    return modules.reduce(function (acc, m) {
        if (acc) return acc
        try { return require(m) } catch (e) { return null }
    }, null)
}
loadFirst([ 'this-will-fail', 'this-wont',  'not-even-tried' ])

The third is solved with this['Q'] || this['bluebird'] || this['Promise']. I could even expose this so you can override this after initialization. The problem is that there isn't an easy way to solve the AMD case in the middle. I'd rather not leave that one. If you can help with that part, I'd really consider putting this in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants