Deferred, asynchronous invocation
Website address: http://dojotoolkit.org/reference-guide/1.9/dojo/Deferred.html
Require (["dojo/deferred", "Dojo/dom", "dojo/on", "dojo/domready!"],function(Deferred, Dom, on) {functionasyncprocess () {varDeferred =NewDeferred (); Dom.byid ("Output"). InnerHTML = "I ' m running ..."; SetTimeout (function() {Deferred.resolve ("Success"); }, 1000); returndeferred.promise; } on (Dom.byid ("Startbutton"), "click",function(){ varProcess =asyncprocess (); Process.then (function(results) {Dom.byid ("Output"). InnerHTML = "I ' m finished, and the result was:" +results; }); });});
Chained calls
Require (["dojo/deferred", "Dojo/dom", "dojo/on", "dojo/domready!"],function(Deferred, Dom, on) {functionasyncprocess (msg) {varDeferred =NewDeferred (); Dom.byid ("Output"). InnerHTML + = "<br/>i ' m running ..."; SetTimeout (function() {deferred.resolve (msg); }, 1000); returndeferred.promise; } on (Dom.byid ("Startbutton"), "click",function(){ varProcess = Asyncprocess ("First"); Process.then (function(results) {Dom.byid ("Output"). InnerHTML + = "<br/>i ' m finished, and the result was:" +results; returnAsyncprocess ("Second"); }). Then (function(results) {Dom.byid ("Output"). InnerHTML + = "<br/>i ' m really finished now, and the result is:" +results; }); });});
Reject
Require (["dojo/deferred", "Dojo/dom", "dojo/on", "dojo/domready!"],function(Deferred, Dom, on) {functionasyncprocess (msg) {varDeferred =NewDeferred (); 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); returndeferred.promise; } on (Dom.byid ("Startbutton"), "click",function(){ varProcess =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) {functionasyncprocess () {vartimeout; varDeferred =NewDeferred (function(reason) {cleartimeout (timeout); Dom.byid ("Output"). InnerHTML + = "<br/>i is 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); returndeferred.promise; } on (Dom.byid ("Startbutton"), "click",function(){ varProcess =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 Official Translator Dojo/deferred