代码改变世界

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

2013-06-20 15:46  【当耐特】  阅读(1629)  评论(5编辑  收藏  举报

使用场景

在一些时候,希望一件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(总)