Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.finished Promise does not exist in Safari #206

Open
tomciopp opened this issue Apr 25, 2019 · 2 comments
Open

.finished Promise does not exist in Safari #206

tomciopp opened this issue Apr 25, 2019 · 2 comments

Comments

@tomciopp
Copy link

If you run a sequence of promises and you are relying on finished property it will fail in Safari.
After doing some research, I stumbled upon this polyfill that fixes the issue and am pasting it here in case it helps anyone else. Ideally this library should include this code as well.

// only polyfill .finished in browsers that already support animate()
if (document.body.animate) {

  // Chrome does not seem to expose the Animation constructor globally
  if (typeof Animation === "undefined") {
    window.Animation = document.body.animate({}).constructor;
  }

  if (!Animation.prototype.hasOwnProperty("finished")) {
    Object.defineProperty(Animation.prototype, "finished", {
      get() {
        if (!this._finished) {
          this._finished = this.playState === "finished" ?
            Promise.resolve() :
            new Promise((resolve, reject) => {
              this.addEventListener("finish", resolve, { once: true });
              this.addEventListener("cancel", reject, { once: true });
            });
        }
        return this._finished;
      }
    });
  }
}
@motss
Copy link

motss commented Apr 26, 2019

Not only Safari. Chrome does not support that too. Alternatively, you can leverage finish event to achieve the same result:

const divEl = document.body.querySelector('.animatable');
const fadeOutAnimation = new Promise(yay => divEl.animate([
 { opacity: '1' },
 { opacity: '0' },
]).onfinish = yay);

fadeOutAnimation.then(() => /** Do more stuff */);

@TechQuery
Copy link

#33 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants