javascript - Re-resolving a jquery.Deferred (or other promise) -
afaik deferreds can resolved once, have scenario i'm storing lot of deferreds in table. it's possible these deferreds' values need updated but other modules want store local reference original deferred, can't overwrite entire deferred new object.
is there way can overwrite deferred's value or if (as suspect) it's inadvisable (even impossible) there tried , tested approaches problem? here's skeleton code illustrate problem
// articles module var articles = {}, // anid = 'uuid'; articles[anid] = $.get('/articles/' + anid); //another module var localcopyofarticle, getarticle = function (anid) { localcopyofarticle = articles[anid] } getarticle('uuid'); // articles articles[anid] = $.get('/articles/refresh/' + anid); //another module again console.log(localcopyofarticle) // oh no - it's out of date
afaik deferreds can resolved once
correct. promise transitioned pending state fulfilled or rejected states (aka resolved) can no longer change state. promise specification says explicitly:
when fulfilled/rejected, promise must not transition other state.
so, there have it, no - promises not change states after being resolved. function returned value doesn't throw exception in retrospect. whenever you're unsure - think sync code analogy
var x = foo(); // returns correctly // .. more code
you wouldn't expect foo
throw after returned or return after threw wouldn't expect promise reject after resolved or resolve after rejected.
is there way can overwrite deferred's value or if (as suspect) it's inadvisable (even impossible) there tried , tested approaches problem? here's skeleton code illustrate problem
no, promise abstration on single computation. settling twice makes no sense. seems wrong abstraction goal.
promises great flow control mechanism they're not intended solve every problem asnychronosu code has deal with. not event emitters one.
this problem can solved promises rather easily:
// articles module var articles = {}, // anid = 'uuid'; articles.get = function(anid){ if(articles[anid]){ return $.when(articles[anid]); // return cached copy } return $.get('/articles/' + anid).then(function(article){ articles[anid] = article; return article; }); } articles.refresh = function(anid){ return $.get('/articles/refresh/' + anid).then(function(article){ articles[anid] = article; return article; }); }); //another module var localcopyofarticle, getarticle = function (anid) { return localcopyofarticle = articles.get(anid); // promise } // articles // .then here __crucial__ since refresh operation async articles.refresh(anid).then(function(article){ console.log(article); // fresh return getarticle(anid) }).then(function(article){ console.log(article); // getarticle, fresh! });
this code has lot of 'cruft' illustrate point. important thing realize operations new operation each time. fetching data new operation each time , not same one. in synchronous code. store values in map , not promises , fetch operation should promise based.
Comments
Post a Comment