toddbluhm toddbluhm - 1 year ago 91
Node.js Question

Whats the difference between Q Promise library's .finally() and .done()?

What is the difference between using the Nodejs Q promise library's .finally() and .done() statements.

For example whats the difference between these two?

.finally(); //or fin()


Answer Source

promise.done(onSuccess, onError) plain simply allows you to process resolved value. Additional benefit is that doesn't imply any error swallowing (as it's case with promise.then), it guarantees that any involved exception would be exposed. It also effectively ends chain and doesn't return any further promise.

promise.finally(fn) is for registering a task that must be done after given promise resolves (it doesn't matter whether promise succeds or fails). Usually you use it for some kind of cleanup operations e.g. imagine you setup a progress bar, that needs to be hidden after request is done (no matter if it was successful), then just do promise.finally(hideProgressBar). Additionally finally returns input promise, so you can return it for further processing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download