• dojo 官方翻译 dojo/Deferred


    延迟,异步调用

    官网地址:http://dojotoolkit.org/reference-guide/1.9/dojo/Deferred.html

    require(["dojo/Deferred", "dojo/dom", "dojo/on", "dojo/domReady!"],
    function(Deferred, dom, on){
      function asyncProcess(){
        var deferred = new Deferred();
    
        dom.byId("output").innerHTML = "I'm running...";
    
        setTimeout(function(){
          deferred.resolve("success");
        }, 1000);
    
        return deferred.promise;
      }
    
      on(dom.byId("startButton"), "click", function(){
        var process = asyncProcess();
        process.then(function(results){
          dom.byId("output").innerHTML = "I'm finished, and the result was: " + results;
        });
      });
    
    });

    链式调用

    require(["dojo/Deferred", "dojo/dom", "dojo/on", "dojo/domReady!"],
    function(Deferred, dom, on){
      function asyncProcess(msg){
        var deferred = new Deferred();
    
        dom.byId("output").innerHTML += "<br/>I'm running...";
    
        setTimeout(function(){
          deferred.resolve(msg);
        }, 1000);
    
        return deferred.promise;
      }
    
      on(dom.byId("startButton"), "click", function(){
        var process = asyncProcess("first");
        process.then(function(results){
          dom.byId("output").innerHTML += "<br/>I'm finished, and the result was: " + results;
          return asyncProcess("second");
        }).then(function(results){
          dom.byId("output").innerHTML += "<br/>I'm really finished now, and the result was: " + results;
        });
      });
    
    });

    reject

    require(["dojo/Deferred", "dojo/dom", "dojo/on", "dojo/domReady!"],
    function(Deferred, dom, on){
      function asyncProcess(msg){
        var deferred = new Deferred();
    
        dom.byId("output").innerHTML += "<br/>I'm running...";
    
        setTimeout(function(){
          deferred.progress("halfway");
        }, 1000);
    
        setTimeout(function(){
          deferred.resolve("finished");
        }, 2000);
    
        setTimeout(function(){
          deferred.reject("ooops");
        }, 1500);
    
        return deferred.promise;
      }
    
      on(dom.byId("startButton"), "click", function(){
        var process = asyncProcess();
        process.then(function(results){
          dom.byId("output").innerHTML += "<br/>I'm finished, and the result was: " + results;
        }, function(err){
          dom.byId("output").innerHTML += "<br/>I errored out with: " + err;
        }, function(progress){
          dom.byId("output").innerHTML += "<br/>I made some progress: " + progress;
        });
      });

    cancel

    require(["dojo/Deferred", "dojo/dom", "dojo/on", "dojo/domReady!"],
    function(Deferred, dom, on){
      function asyncProcess(){
        var timeout;
        var deferred = new Deferred(function(reason){
          clearTimeout(timeout);
          dom.byId("output").innerHTML += "<br/>I was cancelled with reason: " + reason;
        });
    
        dom.byId("output").innerHTML += "<br/>I'm running...";
    
        timeout = setTimeout(function(){
          dom.byId("output").innerHTML += "<br/>My process completed!";
          deferred.resolve("finished");
        }, 2000);
    
        return deferred.promise;
      }
    
      on(dom.byId("startButton"), "click", function(){
        var process = asyncProcess();
        process.then(function(results){
          dom.byId("output").innerHTML += "<br/>I'm finished, and the result was: " + results;
        });
    
        setTimeout(function(){
          process.cancel("goodbye");
        }, 1000);
      });
    
    });
  • 相关阅读:
    qt映射器QSignalMapper的理解
    win10环境下安装Qt4.8、PyQt及development tools
    BZOJ1040 基环森林 找环+基础树形DP
    贪心 BZOJ1034
    HDU5293 树链剖分+树形DP
    BZOJ 1028 BZOJ 1029 //贪心
    BZOJ1025
    仙人掌图判定及求直径HDU3594 BZOJ1023
    BZOJ1021
    BZOJ1022
  • 原文地址:https://www.cnblogs.com/tiandi/p/4492296.html
Copyright © 2020-2023  润新知