How to use requestAnimationFrame with Promise

Viewed 2602

I want to use requestAnimationFrame with Promise, so I can use it this way

opacityToggle("layername",0).then(i=>"do some stuff after animation ends")

, but not sure how to do this. Here is my code:

function opacityToggle(layerName, opacity) {

    if (!layerName) return;
    var requestID;
    var s = 0;
   
    return animate().then(i => i)

    function animate() {
        requestID = requestAnimationFrame(animate);
        if (s < 1) {
            s += 0.01
            s = +s.toFixed(2)
            console.log('s', layerName, s, opacity);
            map.setPaintProperty(layerName, 'fill-opacity', s);
        } else {
            cancelAnimationFrame(requestID);
            return new Promise(resolve => resolve)
        }
    }
}

2 Answers

While @Terry has the simple solution of wrapping just everything in the promise constructor, you get the real power of promises if you promisify only the requestAnimationFrame call itself:

async function opacityToggle(layerName, opacity) { /*
^^^^^ */
    if (!layerName) return;

    var s = 0;
    while (s < 1) {
//  ^^^^^
        s += 0.01
        s = +s.toFixed(2)
        console.log('s', layerName, s, opacity);
        map.setPaintProperty(layerName, 'fill-opacity', s);

        await new Promise(resolve => {
//      ^^^^^
            requestAnimationFrame(resolve);
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        });
    }
}

If you can't use async/await and/or want to do it with the recursive approach, it would be

function opacityToggle(layerName, opacity) {
    if (!layerName) return Promise.resolve();

    var s = 0;
    return animate();

    function animate() {
        if (s < 1) {
            s += 0.01
            s = +s.toFixed(2)
            console.log('s', layerName, s, opacity);
            map.setPaintProperty(layerName, 'fill-opacity', s);
            return new Promise(resolve => {
//          ^^^^^^
                requestAnimationFrame(resolve);
            }).then(animate);
//            ^^^^^^^^^^^^^^
        } else {
            return Promise.resolve();
        }
    }
}

You're slightly over-complicating the return statement of your opacityToggle function. You simply return the promise and encapsulate all the RAF logic inside of it. This should be sufficient:

function opacityToggle(layerName, opacity) {

    if (!layerName) return;
    var requestID;
    var s = 0;

    return new Promise(resolve => {
        // Animation steps
        function animate() {
            if (s < 1) {
                s += 0.01
                s = +s.toFixed(2)
                console.log('s', layerName, s, opacity);
                map.setPaintProperty(layerName, 'fill-opacity', s);

                requestID = requestAnimationFrame(animate);
            } else {
                // If animation is complete, then we resolve the promise
                cancelAnimationFrame(requestID);
                resolve();
            }
        }

        // Start animation
        requestID = requestAnimationFrame(animate);
    });
}

It is unclear in your question tho, of what value you want to resolve the promise with. You can simply provide it as resolve(yourResolvedValueHere) if you want to do so.

Related