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);
  });

});

 

posted @ 2015-05-10 15:25  JackGIS  阅读(611)  评论(0编辑  收藏  举报