0

I'm trying to make my library work in jQuery 3.0 and it don't work because of breaking changes in Deferred objects, I have code like this:

    function ready(defer) {
        return function(fun) {
            if (defer.state() !== 'resolved') {
                defer.then(fun).fail(function(e) {
                    self.error(e);
                });
            } else {
                fun.call(fun);
            }
        };
    }

and I have two methods pause and resume

        pause: function(visible) {
            if (self.id() == 4) {
                console.log('pause call');
            }
            cmd_ready(function ready() {
                onPause();
                paused = true;
                if (self.id() == 4) {
                    console.log('pause', paused);
                }
                command_line.disable();
                if (!visible) {
                    command_line.hidden();
                }
                if ($.isFunction(settings.onPause)) {
                    settings.onPause.call(self);
                }
            });
            return self;
        },
        resume: function() {
            if (self.id() == 4) {
                console.log('resume call');
            }
            cmd_ready(function ready() {
                paused = false;
                if (self.id() == 4) {
                    console.log('resume', paused);
                }
                if (terminals.front() === self) {
                    command_line.enable();
                } else {
                    if (self.id() == 4) {
                        console.log('not front');
                    }
                }
                command_line.visible();
                var original = delayed_commands;
                delayed_commands = [];
                for (var i = 0; i < original.length; ++i) {
                    self.exec.apply(self, original[i]);
                }
                self.trigger('resume');
                var fn = resume_callbacks.shift();
                if (fn) {
                    fn();
                }
                scroll_to_bottom();
                if ($.isFunction(settings.onResume)) {
                    settings.onResume.call(self);
                }
            });
            return self;
        },

The deferred is needed because I'm executing pause before command_line object is created.

and I call resolve after first pause:

        if (self.id() == 4) {
            console.log('resolve');
        }
        command_defer.resolve();

and the order of logs like this:

pause call
resolve
resume call
resume false
resume call
resume false
pause true

the order should look like this:

pause call
resolve
pause true
resume call
resume false
resume call
resume false

why pause then callback is called long after resolve is called? it was working in jQuery 2.

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • 1
    That `ready` method is a horrible thing to do. It should be `return defer.then(fun);`, no branching on anything. – Bergi May 02 '17 at 14:39
  • Please post a [mcve] – Bergi May 02 '17 at 14:42
  • @Bergi I wanted to have the code that will be called synchronously like without the deferred object. – jcubic May 02 '17 at 14:43
  • 1
    Yes, I can see that you wanted that, and it's exactly [what you should not do](http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony) – Bergi May 02 '17 at 14:44
  • When you have added how you are calling this code, please also show the expected result not just the actual one. – Bergi May 02 '17 at 14:44
  • Ok, this one is resolved the `resolve();` callback don't call then synchronously. it's breaking change in jQuery 3.0 Deferred objects. – jcubic May 02 '17 at 14:46
  • @Bergi the promise is only for case when someone call pause before command_line object is created on initialization, it actually should be synchronous. The user code would look weird if it need to call then after each method. – jcubic May 02 '17 at 14:49
  • Yes, that's a breaking change, but it was a deficiency of jQuery and never a good design to rely on synchronous callbacks. – Bergi May 02 '17 at 14:50
  • You might want to use http://api.jquery.com/jQuery.Callbacks/ directly – Bergi May 02 '17 at 14:51
  • This is a horrible scheme. You're using a piece of promises and then trying to reinvent a whole bunch of other functionality that already exists in promises and, in the process, are making things about 10x more complicated than they need to be. – jfriend00 May 02 '17 at 20:26

0 Answers0