node.js - What happens if i reject / resolve multiple times in Kriskowal's q? -
i'm studying promises pattern , using kriskowal's q node.js,
having snippet:
var deferred = q.defer(); try { messagedata = json.parse(message); } catch (e) { global.logger.warn('error parsing json message.'); deferred.reject(e); } ... if (some_reason) deferred.resolve(something); ... return deferred.promise;
what if both parser fails , some_reason true?
will execution procede rejecting through resolving , both promise's method called @ different times, generating bug?
should avoid call reject/resolve multiple times?
since promises can resolve once (to either fulfilled or rejected), first resolution wins , further calls ignored. the docs:
in cases promise resolved (i.e. either fulfilled or rejected), resolution permanent , cannot reset. attempting call resolve, reject, or notify if promise resolved no-op.
should avoid call reject/resolve multiple times?
you can design application letting 2 methods "race" against each other resolve deferred, in general should avoided reduce confusion of reader.
Comments
Post a Comment