javascript - When would someone need to create a deferred? -
it seems creating deferred objects commonly discouraged in favor of using es6-style promise constructor. there exist situation necessary (or better somehow) use deferred?
for example, on this page, following example given justification using deferred:
function delay(ms) { var deferred = promise.pending(); settimeout(function(){ deferred.resolve(); }, ms); return deferred.promise; }
however, done promise constructor:
function delay(ms) { return new promise(function(resolve, reject){ settimeout(function(){ resolve(); }, ms); }); }
does there exist situation necessary (or better somehow) use deferred?
there no such situation deferred necessary. "better" matter of opinion won't address here.
there's reason es6 promise specification not have deferred object. you don't need one. people used use deferred object can always done way doesn't use deferred object.
first off, majority of uses fit nicely promise constructor model. secondly, other cases didn't fit quite cleanly model can still accomplished promise constructor or starting resolved promise , chaining it.
the main use case i've seen deferred when want pass ability resolve()
or reject()
off other piece of code other code created promise. deferred made easy since pass deferred object , had public methods resolving or rejecting it. but, promise, can pass resolve
and/or reject
methods. since bound specific object automatically, can pass function references. and, in other cases, can let other code create own promise , resolve/reject , have operation linked yours rather letting them resolve/reject promise. quite clean passing deferred object? matter of opinion, neither common use cases , can accomplished without separate deferred object.
and, torazaburo points out, letting external code resolve or reject promise bit of anti-pattern in own right. created promise - resolve/reject it. if want use external events decide when resolve/reject it, have them notify (via own promise or callback) , can resolve/reject own promise. or, have them create own promise can use. that's desired model. or, let them chain onto promise end result gated when operation done.
if 1 used coding deferred object (say jquery deferred), might take little getting used coding without it, after little while start think differently , starts come natural use promise constructor.
once promisify sphere of async operations use in given application, it's pretty rare ever need create own promises more because building off promises async functions call creating or using flow control operations promise.all()
create uber promises you.
this main point of anti-patterns. use promises created rather manually create more. chain them. return promises .then()
handlers link async operations under logic control.
of course, there existing async operations don't return promises needs create promise, should done in promisify layer somewhere outside purvey of main coding logic , done once operation , code calls async operation should able use returned promises.
Comments
Post a Comment