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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -