I'm trying to get a series of events to occur in the browser one after the other, but JavaScript being asynchronous makes that difficult. I'm using jQuery to handle adding and removing classes, and I want to use standard JS promises to make everything occur in sequence but I'm having a hard time of things. What I'm trying to do is wrap the jQuery event in Promise.resolve() and use that to call .then() (I've had success with this in the past with AJAX calls.) For example:
makePromise = function() {
return Promise.resolve($(".mydiv").addClass("grow").promise());
}
makePromise().then(function() {
alert("running");
})
.mydiv {
height: 100px;
width: 100px;
background-color: blue;
}
.mydiv.grow {
transition: width 2s;
width: 500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mydiv">
</div>
The intent is for the jQuery event to run it's course, then run what's going on in the then() call, but as you can see it's marching right on into the next step without waiting. Is there a way to make this play work or am I going in the wrong direction entirely? Help me Stack Overflow Hivemind, you're my only hope.