0

我正在应用这个名为 noty 的库,我想解决这个承诺。这是代码。

    var bouncejsClose = function (promise) {
    var n = this;
    new Bounce()
        .translate({
            from: { x: 0, y: 0 }, to: { x: 450, y: 0 },
            easing: 'bounce',
            duration: 500,
            bounces: 4,
            stiffness: 1
        })
        .applyTo(n.barDom, {
            onComplete: function () {
                promise(function (resolve) {
                    resolve();
                });
            }
        });
};
4

1 回答 1

1

您无法解析其声明的Promisefrom outside,您需要做的是:


var bouncejsClosePromise = new Promise((resolve, reject) => {
  var n = this;
  new Bounce()
    .translate({
      from: { x: 0, y: 0 },
      to: { x: 450, y: 0 },
      easing: "bounce",
      duration: 500,
      bounces: 4,
      stiffness: 1
    })
    .applyTo(n.barDom, {
      onComplete: function () {
        //now you can resolve here
        resolve(/** with any value you want to get when calling .then method */)
      }
    });
});

bouncejsClosePromise.then((/** if you pass value to the resolve callback, you can get it here */) => {
  //your promise successfully resolved
}).catch(() => {
  //or in case you used reject in your promise, you can handle it here
})
于 2022-01-19T06:27:25.967 回答