reflux / reflux-core

A simple core library for uni-directional dataflow application architecture inspired by ReactJS Flux
BSD 3-Clause "New" or "Revised" License
48 stars 19 forks source link

Async actions couldn't return promise properly in IE even with Reflux.setPromiseFactory has been called before. #9

Closed kpaxqin closed 9 years ago

kpaxqin commented 9 years ago

I've meet this issue recently:

//set promise factory for IE .
Reflux.setPromiseFactory(/* promise factory */);

const myAction = Reflux.createAction({ asyncResult: true });

myAction(payload).then(function(){});
//got error in IE , cannot call method then of undefined

and the only way to work properly is

myAction.triggerPromise(payload).then(function(){});

I track the code and find that in this line of createAciton.js, we won't call triggerPromise when _.environment.hasPromise returns false.

And when I setPromiseFactory, only _.createPromise has been set, the _.environment.hasPromise still remains false.

This leads different behaviours between IE and Chrome, though I polyfill the Promise support by setPromiseFactory.

Can we fix this issue this way ?

_.noop = function(){};

//line 81 in utils.js
if (environment.hasPromise) {
    exports.Promise = Promise;
    exports.createPromise = function (resolver) {
        return new exports.Promise(resolver);
    };
} else {
    exports.Promise = null;
    exports.createPromise = _.noop;
}

// line 55 in createAction.js
var functor = function() {
        var triggerType = functor.sync ? "trigger" :
            ( _.createPromise !== _.noop ? "triggerPromise" : "triggerAsync" );
        return functor[triggerType].apply(functor, arguments);
    };
kpaxqin commented 9 years ago

Sorry guys, duplicated with Refux will not use promises when the browser doesn't support them even when externally supplied.