码农干货系列【20】--add gtTime to Promise.js


使用场景

在一些时候,希望一件task不能太快完成,需要大于多少时间才可以执行,就可以使用Promise的gtTime方法.

使用方式

        Promise.gtTime(f1(), 5000).then(f2);
        function f1() {
            var promise = Promise();
            setTimeout(function () {

                console.log(1);
                promise.resolve("from f1");
            }, 1500)

            return promise;
        }

        function f2() {
            var promise = Promise();
            setTimeout(function () {

                console.log(2);
                promise.resolve();
            }, 1500)

            return promise;
        }

f1执行只需要1.5秒,但是希望至少5秒后再执行f2,所以使用Promise.gtTime(f1(), 5000).then(f2)。如果f1需要8秒,则无视5秒的限制,8秒后才执行f2。

你可以点击这里下载最新版本。

有关promise更多信息你可以访问:

getting started with Promise.js(总)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM