javascript - got puzzled about promise in jQuery -


1 value can not change in promise example

var t = function(s) {    var wait = function(dtd) {          var dtd = $.deferred();      //new deferred object in function                var tasks = function() {              alert("complete!");        s = s + "hhh";              dtd.resolve(s); // change state of deferred object            };            settimeout(tasks, 5000);      // return promise object      return dtd.promise(s);    };  }    var s = "hhh";  $.when(t(s))    .then(function() {      alert(s);    }).then(function() {      alert(s);    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

i can got "hhh" instead of "hhhhhh"...

2 how invoke promise chain different values?like a.then(b(c)).then(f(d))

i put values in object , pass on chain...

the alert in $.when alerting global variable not resolve in tasks

also , never call wait() , tasks() doesn't return anything.

the return of promise returns wait() never gets called. returning inner function not return outer function

also have no arguments in then() receive resolved data. in order data second then, need return first one

var t = function (s) {      var wait = function () {              var dtd = $.deferred();          //new deferred object in function                        var tasks = function () {                    alert("complete!");              s = s + "hhh";                    dtd.resolve(s); // change state of deferred object                };                settimeout(tasks, 2000);          // return promise object          return dtd.promise();      };     // return promise inside `wait`      return wait()  }    var s = "hhh";  $.when(t(s)).then(function (resolveddata) {      // return next then...just because can      return resolveddata; // must return if want access in next          }).then(function(previousthendata) {             alert(previousthendata);// alert argument  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>


Comments

Popular posts from this blog

How to show in django cms breadcrumbs full path? -

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -