简体   繁体   English

将jQuery延迟承诺转换为原生JavaScript承诺?

[英]Converting a jQuery Deferred Promise to a Native JavaScript Promise?

I plucked this JavaScript code below out of a library and it uses jQuerys Deferred/promises which are not the same as the official Promise spec. 我从库中抽取了以下JavaScript代码,它使用的jQuerys Deferred / promises与官方的Promise规范不同。

I would appreciate any help in simply converting this basic example into a Native JavaScript Promise while keeping the same format/structure below. 我将在将以下基本示例转换为Native JavaScript Promise的同时,保持下面相同的格式/结构的任何帮助将不胜感激。

Almost every example Promise usage I find online is of loading something Asynchronously and I have some cases where I would like to use them for other tasks and this example does just that! 我在网上找到的几乎每个示例Promise用法都是异步加载某些内容,在某些情况下,我想将它们用于其他任务,而本示例正是这样做的!

  • App.show() is called App.show()被调用
  • App.show() calls a function that returns a Promise App.Animations.swipe.apply(this, [current, next, dir]).then(finalize); App.show()调用返回Promise App.Animations.swipe.apply(this, [current, next, dir]).then(finalize);的函数App.Animations.swipe.apply(this, [current, next, dir]).then(finalize);
  • The promise function has an event handler function inside it that runs when a CSS animation is completed and that triggers the Promise to resolve. promise函数内部有一个事件处理函数,该事件处理函数在CSS动画完成时运行,并触发Promise进行解析。
  • When the promise resolves it then calls the finalize function that is inside of the App.show() function. 当承诺解决它,然后调用finalize函数是内部App.show()函数。

It might be easier to follow what it does by simply looking at the example below.... 只需看下面的例子,可能会更容易理解它的作用。

var App = {

  show: function(index, direction) {
    var $this = this;

    // called after Promise is resolved with .then(finalize)
    finalize = function() {
      // other code here ran now after promise resolved from App.Animations.swipe()....
    };

    // call function which returns a Promise
    Animations.swipe.apply(this, [current, next, dir]).then(finalize);
  },


  Animations = {

    'none': function() {
      var d = UI.$.Deferred();
      d.resolve();
      return d.promise();
    },

    // function returns a Promise which has an event handler inside it that resolves the promise
    'swipe': function(current, next, dir) {

      var d = UI.$.Deferred();

      next.css('animation-duration', this.options.duration + 'ms');

      // Event handler ran one time when CSS Animation ends and triggers the event
      // this event is what resolves the promise
      next.css('opacity', 1).one(UI.support.animation.end, function() {

        current.removeClass(dir === -1 ? 'uk-slideshow-swipe-backward-out' : 'uk-slideshow-swipe-forward-out');
        next.css('opacity', '').removeClass(dir === -1 ? 'uk-slideshow-swipe-backward-in' : 'uk-slideshow-swipe-forward-in');
        d.resolve();

      }.bind(this));

      // return a Promise Object when this function is called
      return d.promise();
    },
  }
};

So based on that code how can it be converted to not use jQuerys Deferred and use Native JS Promises? 因此,基于该代码,如何将其转换为不使用延期的jQuery和使用本机JS Promises?

'none': function() {
  var d = UI.$.Deferred();
  d.resolve();
  return d.promise();
},

That's a simple one 那很简单

'none': function() {
    return Promise.resolve();
},

Now for swipe: 现在可以滑动:

'swipe': function(current, next, dir) {
  return new Promise(function(resolve, reject) {
    next.css('animation-duration', this.options.duration + 'ms');

    // Event handler ran one time when CSS Animation ends and triggers the event
    // this event is what resolves the promise
    next.css('opacity', 1).one(UI.support.animation.end, function() {

      current.removeClass(dir === -1 ? 'uk-slideshow-swipe-backward-out' : 'uk-slideshow-swipe-forward-out');
      next.css('opacity', '').removeClass(dir === -1 ? 'uk-slideshow-swipe-backward-in' : 'uk-slideshow-swipe-forward-in');
      resolve();

    }.bind(this));
},

I just want to add that jQuery Promises (or Deferreds as they call them) were at one stage kind of broken, but these days I think they are compliant with Promise/A+ spec. 我只想补充一下,jQuery Promises(或叫Deferreds的它们在某种程度上)处于崩溃的阶段,但是现在我认为它们符合Promise / A +规范。

Many jQuery functions (ajax, animations for example) return a promise, or you can return .promise() - which negates the need for what I've done in the answer, and that is to use a promise constructor anti-pattern. 许多jQuery函数(例如ajax,动画)都返回一个promise,或者您可以返回.promise() ,这消除了对我在答案中所做的事情的需要,那就是使用promise构造函数的反模式。

I'm not sure enough about your code to know if any of those jQuery methods can return a promise or not 我对您的代码不太确定,是否知道这些jQuery方法中的任何一个都可以返回诺言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM