What happens if i reject / resolve multiple times in Kriskowal's q?

前端 未结 1 1589
北荒
北荒 2020-11-29 11:03

I\'m studying the promises pattern and using kriskowal\'s q for node.js,

having this snippet:

var deferred = Q.defer();
try {
    messageData = JSON.         


        
相关标签:
1条回答
  • 2020-11-29 11:43

    Since promises can only resolve once (to either fulfilled or rejected), the first resolution wins and any further calls will be ignored. From the docs:

    In all cases where a promise is resolved (i.e. either fulfilled or rejected), the resolution is permanent and cannot be reset. Attempting to call resolve, reject, or notify if promise is already resolved will be a no-op.

    Should i avoid to call reject/resolve multiple times?

    You can even design your application letting two methods "race" against each other to resolve a deferred, but in general it should be avoided to reduce confusion of a reader.

    0 讨论(0)
提交回复
热议问题