体验 Promise 优美的链式写法

古人云:“君子一诺千金”,这种“承诺将来会执行”的对象在 JavaScript 中称为 Promise 对象。Promise 有各种开源实现,在 ES6 中被统一规范,由浏览器直接支持。

浏览器中的 Promise 对象

在下面的代码中通过使用 callback 和 Promise 来体验

  • callback 臭名昭著的邪恶金字塔
  • Promise 优美的链式写法
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>体验 Promise 的链式写法</title>
    <style>
        .ball {
            width: 40px;
            height: 40px;
            border-radius: 20px;
        }

        .ball1, .ball4 {
            background: red;
        }

        .ball2, .ball5 {
            background: green;
        }

        .ball3, .ball6 {
            background: yellow;
        }
    </style>
</head>
<body>
<h1>采用回调函数的方式来实现</h1>

<div class="ball ball1" style="margin-left: 0px;"></div>
<div class="ball ball2" style="margin-left: 0px;"></div>
<div class="ball ball3" style="margin-left: 0px;"></div>
<hr>
<h1>采用 Promise 的方式来实现</h1>

<div class="ball ball4" style="margin-left: 0px;"></div>
<div class="ball ball5" style="margin-left: 0px;"></div>
<div class="ball ball6" style="margin-left: 0px;"></div>

<script>
    var ball1 = document.querySelector('.ball1');
    var ball2 = document.querySelector('.ball2');
    var ball3 = document.querySelector('.ball3');
    var ball4 = document.querySelector('.ball4');
    var ball5 = document.querySelector('.ball5');
    var ball6 = document.querySelector('.ball6');

    //    /**
    //     * 方法一:采用 setTimeout 实现
    //     * @param ball
    //     * @param distance
    //     * @param callback
    //     */
    //    function animate(ball, distance, callback) {
    //        setTimeout(function () {
    //            var marginLeft = parseInt(ball.style.marginLeft);
    //            if (marginLeft == distance) {
    //                callback && callback();
    //                return; // 注意在这里一定要返回定时器对象,确保后面拿到的是一个新的定时器对象
    //            } else {
    //                if (marginLeft < distance) {
    //                    marginLeft++;
    //                } else {
    //                    marginLeft--;
    //                }
    //            }
    //            ball.style.marginLeft = marginLeft + 'px';
    //            animate(ball, distance, callback);
    //        }, 10);
    //    }

    /**
     * 方法二:采用 setInterval 实现
     * @param ball
     * @param distance
     * @param callback
     */
    function animate(ball, distance, callback) {
        var interval = setInterval(function () {
            var marginLeft = parseInt(ball.style.marginLeft);
            if (marginLeft == distance) {
                callback && callback();
                clearInterval(interval);
            } else {
                if (marginLeft < distance) {
                    marginLeft++;
                } else {
                    marginLeft--;
                }
            }
            ball.style.marginLeft = marginLeft + 'px';
        }, 10);
    }

    animate(ball1, 100, function () {
        animate(ball2, 200, function () {
            animate(ball3, 300, function () {
                animate(ball3, 150, function () {
                    animate(ball2, 150, function () {
                        animate(ball1, 150, function () {
                            //
                        });
                    });
                });
            });
        });
    });

    //    /**
    //     * 采用 Promise 实现
    //     * @param ball
    //     * @param distance
    //     * @returns {Promise}
    //     */
    //    function promiseAnimate(ball, distance) {
    //        return new Promise(function (resolve, reject) {
    //            var interval = setInterval(function () {
    //                var marginLeft = parseInt(ball.style.marginLeft);
    //                if (marginLeft == distance) {
    //                    resolve();
    //                    clearInterval(interval);
    //                } else {
    //                    if (marginLeft < distance) {
    //                        marginLeft++;
    //                    } else {
    //                        marginLeft--;
    //                    }
    //                }
    //                ball.style.marginLeft = marginLeft + 'px';
    //            }, 10);
    //        });
    //    }
    //
    //    promiseAnimate(ball4, 100)
    //            .then(function () {
    //                return promiseAnimate(ball5, 200);
    //            })
    //            .then(function () {
    //                return promiseAnimate(ball6, 300);
    //            })
    //            .then(function () {
    //                return promiseAnimate(ball6, 150);
    //            })
    //            .then(function () {
    //                return promiseAnimate(ball5, 150);
    //            })
    //            .then(function () {
    //                return promiseAnimate(ball4, 150);
    //            });

    /**
     * 采用 Promise 实现
     * @param ball
     * @param distance
     * @returns {Function}
     */
    function promiseAnimate(ball, distance) {
        return function () {
            return new Promise(function (resolve, reject) {
                var interval = setInterval(function () {
                    var marginLeft = parseInt(ball.style.marginLeft);
                    if (marginLeft == distance) {
                        resolve();
                        clearInterval(interval);
                    } else {
                        if (marginLeft < distance) {
                            marginLeft++;
                        } else {
                            marginLeft--;
                        }
                    }
                    ball.style.marginLeft = marginLeft + 'px';
                }, 10);
            });
        }
    }

    promiseAnimate(ball4, 100)()
            .then(promiseAnimate(ball5, 200))
            .then(promiseAnimate(ball6, 300))
            .then(promiseAnimate(ball6, 150))
            .then(promiseAnimate(ball5, 150))
            .then(promiseAnimate(ball4, 150))
</script>
</body>
</html>

效果图:


运动的最终结果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 编后吐槽:写的快花眼,很详细,耐心看必受益匪浅 JavaScript的执行环境是「单线程」的。所谓单线程,是指JS...
    果汁凉茶丶阅读 4,667评论 8 27
  • 一.为什么需要promise? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作。其中,既有异...
    overflow_hidden阅读 12,474评论 2 21
  • 本文适用的读者 本文写给有一定Promise使用经验的人,如果你还没有使用过Promise,这篇文章可能不适合你,...
    HZ充电大喵阅读 7,341评论 6 19
  • Promise的含义:   Promise是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和...
    呼呼哥阅读 2,199评论 0 16
  • 这篇文章,是深圳海上世界之行回顾的正片。我把这篇文章单独抽出来,是源于自己大学社团经历的回忆。大雨中搬帐篷搭帐篷再...
    Mr_yuan1994阅读 314评论 0 0