码迷,mamicode.com
首页 > 其他好文 > 详细

dojo 官方翻译 dojo/Deferred

时间:2015-05-10 16:57:47      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

延迟,异步调用

官网地址: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);
  });

});

 

dojo 官方翻译 dojo/Deferred

标签:

原文地址:http://www.cnblogs.com/tiandi/p/4492296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!