javascript - Return statement in promise's then -
i'm learning promises (vanilla es6 promises this shim) , have 2 functions, each of them returning new promise , 3rd function called magic()
, calls 2 functions , returns promise.
sample code:
var creatorx = { "create": function() { var p = new promise(function(resolve, reject) { window.settimeout(function() { window.console.log("created"); resolve(22); }, 100); }); homecoming p; } }; var creatory = { "create": function(id) { window.console.log("id " + id); var p = new promise(function(resolve, reject) { window.settimeout(function() { resolve(2244); }, 5); }); homecoming p; } };
and here's magic:
function magic() { homecoming creatorx.create().then(function(d) { creatory.create(d); // shouldn't 'return creatory...'? }); } var r = magic(); r.then(function() { console.log("done"); }, function(err){console.error("oops")});
my question is, why work? don't have homecoming statement @ line creatory.create(d)
in magic function , yet seems work. should return
there? coincidence works?
when homecoming promise function, promise resolved value return. returned value promised function passed on chained promise function.
in case, not returning explicitly. so, javascript resolve promise undefined
, available
function() { console.log("done"); }
function. ignoring resolved value previous promise. if want create utilize of it, alter function to
function(result) { console.log(result); }
now able see actual resolved value.
javascript promise
No comments:
Post a Comment