zoukankan      html  css  js  c++  java
  • 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);
      });
    
    });
  • 相关阅读:
    团队项目——个人工作任务认领
    第八周进度总结
    Scrapy框架安装与使用(基于windows系统)
    《掌握需求过程》阅读笔记四
    《掌握需求过程》阅读笔记三
    《掌握需求过程》阅读笔记二
    《掌握需求过程》阅读笔记(一)
    MapReduce显示最受欢迎的Top10课程(按照课程编号)
    MapReduce处理数据1
    MongoDB Java操作
  • 原文地址:https://www.cnblogs.com/tiandi/p/4492296.html
Copyright © 2011-2022 走看看