zoukankan      html  css  js  c++  java
  • 码农干货系列【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(总)

  • 相关阅读:
    找球号(一)
    拦截导弹
    开灯问题
    超级台阶
    小学生算术
    Financial Management
    三角形面积
    另一种阶乘问题
    并发环境下,先操作数据库还是先操作缓存?
    Flask框架Server和RequestHandler的爱恨纠缠
  • 原文地址:https://www.cnblogs.com/iamzhanglei/p/3146139.html
Copyright © 2011-2022 走看看