我正在应用这个名为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();
});
}
});
};
您无法从声明的outside
解析Promise
,您需要做的是:
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
})