碼農干貨系列【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