const promise = new Promise((resolve) => {
resolve()
})
function move(y) {
anime({
targets: '.test',
translateY: y
})
}
promise
.then(() => { move(100) })
.then(() => { move(-100) })
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
<div class="test"> Hello world </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js"></script>
I need the .test
element to go up by 100 first and only then to go down. This doesn't happen, so I wrote:
promise
.then(() => {
move(100)
return new Promise((resolve) => {
setTimeout(resolve, 1000)
})
})
.then(() => { move(-100) })
Is there a better solution? On mdn it is explicitly said:
Callbacks added with then() will never be invoked before the completion of the current run of the JavaScript event loop.
so am I doing something wrong?