Вот сердце кода, чтобы сделать это.CodePen, показывающий полный пример, находится по адресу https://codepen.io/david263/pen/MLarQp.
// Wrap entire animation in a Promise
function Animate(AnimStep)
{
var o={}; // Local Promise variables
o.Step=AnimStep.bind(o); // Make o.Step() have this==o
return new Promise(function(resolve,reject)
{
// Remember some local variables
o.resolve=resolve;
o.start=0;
o.id=W.requestAnimationFrame(o.Step);
});
} // Animate
// Define animation step
function AnimStep(timestamp)
{
// Fastest MsecPerFrame with lots of animation is 10 msec (60 fps)
// Note that css transform is done without smoothing
var MsecPerFrame=10, MsecPerAnim=2000;
if (!this.start)
this.start=timestamp;
var progress = timestamp - this.start;
GreenBarEl.style.left=progress / MsecPerFrame + 'px';
if (progress < MsecPerAnim)
W.requestAnimationFrame(this.Step);
else
this.resolve();
} // AnimStep
// Run the animation and signal when done
Animate(AnimStep).then(Done);