Skip to content

Instantly share code, notes, and snippets.

bergus /
Last active August 29, 2015 14:16
Promise Cancellation


Cancellation is not an error and not a fulfillment. It means that the resolution value is no more needed, the underlying process to compute it can and shall be aborted (like an XMLHttpRequest). The callbacks just don't get called any more, there is no error passing through the errorback chain.

For this, callbacks which are attached to a promise can be revoked so that they will not be called regardless what happens to the promise - as if it was forever pending. To support branching of promise chains without introducing unexpected cancellations, every promise keeps track of how many callbacks are attached and not revoked. When it is attempted to be cancelled, it can ensure that no callbacks are interested in the result any more. After asserting this, it can (and should) attempt to cancel all other promises that it depends on (or alternatively, abort the non-promise primitive it is built for). Mid-chain cancellation attempts are not effective in this scenario.

The design is built on two pil

bergus / gist:1387854
Created November 23, 2011 04:01 — forked from lsauer/gist:1305056
Correct easiest way to find duplicate values in a JavaScript array - Native unique function implementation
* I saw this thread:
* The solutions from gist:1305056 were elegant, but wrong. So here's mine:
Array.prototype.unique = function(test) {
/* returns a new, sorted Array without duplicates */
if (!Array.isArray(this))
throw new TypeError("Array.prototype.unique must be called on an Array");
return this.slice(0).sort().filter( typeof test == "function"
? function(v, i, a) { return !i || !test(v, a[i-1]); }