有没有办法控制线性呼叫顺序中的承诺流量? [英] Is there a way to control flow of promises in linear call order?

查看:71
本文介绍了有没有办法控制线性呼叫顺序中的承诺流量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然没有紧紧抓住承诺。假设我有一个代码:

I still don't have tight grip on promises. Say I have a code:

selected.eq(i)   // blink
    .fadeOut( 200 ).delay( 50 ).fadeIn( 400 )
    .delay( 100 )
    .fadeOut( 200 ).delay( 50 ).fadeIn( 400 );

以后我打电话:

selected.unbind('click')
    .promise().done(function ()
        {
            selected.fadeOut(500);
        });

它的作用是例外 - 一旦闪烁完成,最后一个 fadeOut 开始了。但是当我通过插件闪烁时(设置使用jQuery动画)所以我的第一部分只是一个电话:

It works as excepted -- once the blinking is done, the last fadeOut kicks in. But when I do the blinking via plugin (with setup to use jQuery animation) so my entire first part is just one call:

selected.eq(i).modernBlink({ 
    duration: 750,
    iterationCount: 3,
    auto: true}
    );

眨眼的主体是:

ModernBlink.prototype._fallbackAnimation = function _fallbackAnimation( iterationCount ) {
    var self = this,
    duration = this.options.duration / 2;

    if ( iterationCount > 0 || iterationCount === 'infinite' ) {
        iterationCount = iterationCount === "infinite" ? "infinite" : iterationCount - 1;

    this.el.animate( { 'opacity': 0 }, duration ).promise().done( function() {
        self.el.animate( { 'opacity': 1 }, duration );
        self._fallbackAnimation( iterationCount );
    });
}
};

所以它是基于承诺的递归调用同样。结果是不同的 - 在第一次从 blink 迭代后,我的代码启动,因为我的承诺赢了。

so it is recursive call based on promise as well. The outcome is different -- after first iteration from blink my code kicks in, because my promise won.

视觉效果是 - 眨眼,淡出(我的部分),保持眨眼。

The visual effect is -- blink, fade out (mine part), keep blinking.

我的问题是 - 如何告诉jQuery 闪烁承诺比我的更重要?

My question is -- how to tell jQuery the blink promises are more important than mine?

注意:我不能直接将第二部分附加到第一部分,因为它们分散在代码中,有时第一部分不会被执行。

NOTE: I cannot attach directly the second part to the first part, because they are scattered in code, and sometimes the first part is not executed.

眨眼代码来自leonderijke的 Modern Blink 。 MB在这里被用作例子!

The code of blink comes from Modern Blink by leonderijke. MB was used here as example!

更新:大图,我不知道这是否有用,但无论如何:

UPDATE: Big picture, I don't know if this helpful or not, but anyway:

if (Environment.Mode=='blink') // my private settings variable
    call_blink(); // part 1, presented above
call_fade_out(); // part 2, presented above

这就是为什么我无法将它们链接起来。我有类似的配置代码使用更多,所以我想了解它,并在这里和其他地方使用它。

This is why, I cannot chain them. I have similar "configuration" of code used some more, so I would like to understand it, and use it here and in other places.

推荐答案

您需要使 modernBlink 返回代表结果你是在 - 眨眼结束。根本不可能拦截将来只会链接的动画。你正在使用的插件应该返回一个承诺,或者至少提供一个回调;如果不是那样你就需要使用另一个或叉子。

You will need to make modernBlink return a promise that represents the result you are after - the end of the blinking. It simply is impossible to intercept animations that will only be chained in the future. The plugin you're using should return a promise, or at least provide a callback; if it doesn't to that you'll need to use a different one or fork it.

在你的情况下,blink方法需要反复链接到承诺使用 然后

In your case, the blink method will need to repeatedly chain itself to the promise by using then:

ModernBlink.prototype._fallbackAnimation = function(iterationCount) {
    var self = this,
    duration = this.options.duration / 2;

    if (iterationCount === 'infinite' || iterationCount-- > 0) {
        return this.el.animate({'opacity': 0}, duration).promise()
        .then(function() {
            return self.el.animate({'opacity': 1}, duration).promise();
        })
        .then(function() {
            return self._fallbackAnimation(iterationCount);
        });
    } else {
        return this;
    }
};

现在,您的大图可能如下所示:

Now, your big picture might look like this:

if (Environment.Mode=='blink')
    var step1 = new ModernBlink()._fallbackAnimation(5);
else
    var step1 = jQuery.when(undefined);

var step2 = step1.then(call_fade_out);

这篇关于有没有办法控制线性呼叫顺序中的承诺流量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆