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

Correct way to use mojs with promise/async? #221

Open
uduse opened this issue Aug 25, 2019 · 4 comments
Open

Correct way to use mojs with promise/async? #221

uduse opened this issue Aug 25, 2019 · 4 comments
Labels
feature feature request to implement
Projects

Comments

@uduse
Copy link

uduse commented Aug 25, 2019

I want to play an animation and wait until it finishes. I checked the docs and tutorials and I did not find an API that returns a promise or something.

It seems there's no related discussion here so I would like it to ask if there's a standard way of doing this?

What I am doing now is wrapping it in a promise and it works fine:

const burstPromise = new Promise(resolve => {
    const burst = new mojs.Burst({
        radius: {30: 120},
        count: 10,
        children: {
            duration: 3000,
        },
        onComplete() {
            resolve();
        }
    });
    burst.play();
});

await burstPromise;
@Sandstedt
Copy link
Member

I haven't seen anything else, so I think this solution is the way to go. Mojs is not (to my knowledge) promise based at all, instead relies on callback functions. But maybe it could be a interesting feature. I personally never have needed something similar.

In what use case do you need this promise to resolve? Maybe we should always return a promise when the animation is finished?

@Sandstedt Sandstedt added question needing further explanations feature feature request to implement help need help, any contribution are welcome labels Aug 26, 2019
@uduse
Copy link
Author

uduse commented Aug 26, 2019

USE CASE: I am designing a game and using frontend technology for rapid prototyping (similar to Zelda creating a 2D version for testing before implementing a full-fledged 3D version). An entity plays a burst animation once hit and only after that animation is done, the next animation shall be played (or updating the board state or stuff).

I am using animejs as well and it supports waiting by returning a promise, so I can do await someAnimation.finished. This feature could be nice for mojs as well.

So instead of the code above, I can do:

const burst = new mojs.Burst({
 radius: {30: 120},
 count: 10,
});

await burst.play().finished;

@Sandstedt
Copy link
Member

Thanks for the use case (and interesting use of the library 🙂).

But yeah, then I can understand it's a bit easier to to sequencial code if you can use it that way. We'll look into it.

@xavierfoucrier
Copy link
Member

I confirm I am using a Promise too with the onComplete event of mojs if I need to wait for the animation to complete because it does not support Promise for now 😉

@xavierfoucrier xavierfoucrier added this to Features in mojs@next Feb 13, 2020
@xavierfoucrier xavierfoucrier removed help need help, any contribution are welcome question needing further explanations labels Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature feature request to implement
Projects
No open projects
mojs@next
Features
Development

No branches or pull requests

3 participants