• 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);
      });
    
    });
  • 相关阅读:
    本周总结
    本周总结
    本周总结
    本周总结
    性能分析(4)
    大型网站高性能架构
    第二天大数据清洗
    性能分析(2)
    六大质量属性——性能分析(1)
    java设计模式9-代理模式
  • 原文地址:https://www.cnblogs.com/tiandi/p/4492296.html
Copyright © 2020-2023  润新知