Skip to content

Instantly share code, notes, and snippets.

Bergi bergus

Block or report user

Report or block bergus

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bergus
bergus / CancellationToken+.md
Last active Aug 29, 2015
Promise Cancellation
View CancellationToken+.md

Rationale

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
bergus / gist:1387854
Created Nov 23, 2011 — forked from lsauer/gist:1305056
Correct easiest way to find duplicate values in a JavaScript array - Native unique function implementation
View gist:1387854
/*
* I saw this thread: http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array
* 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]); }
You can’t perform that action at this time.