Created
March 31, 2013 23:28
-
-
Save novemberborn/5282453 to your computer and use it in GitHub Desktop.
Crappy examples for <https://github.com/novemberborn/legendary/tree/cancellation-c>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var legendary = require("legendary"); | |
test1(); | |
test2(); | |
test3(); | |
test4(); | |
test5(); | |
test6(); | |
test7(); | |
function test1(){ | |
// Create a pending promise p1 | |
var p1 = new legendary.Promise(function(resolve){ | |
setTimeout(resolve, 2000, "test1"); | |
}); | |
p1.then(function(value){ | |
console.log("1.1:", value); | |
}, function(reason){ | |
console.log("1.1:", reason, reason.stack); | |
throw reason; | |
}); | |
var p2 = p1.then(null, function(reason){ | |
console.log("1.2:", reason, reason.stack); | |
throw reason; | |
}); | |
p2.then(null, function(reason){ | |
console.log("1.3:", reason, reason.stack); | |
}); | |
// Cancelling p1 means that all derived promises get cancelled too. | |
p1.cancel(); | |
} | |
function test2(){ | |
// Create a pending promise p1 | |
var p1 = new legendary.Promise(function(resolve){ | |
setTimeout(resolve, 2000, "test2"); | |
}); | |
p1.then(function(value){ | |
console.log("2.1:", value); | |
}, function(reason){ | |
console.log("2.1:", reason, reason.stack); | |
throw reason; | |
}); | |
var p2 = p1.then(null, function(reason){ | |
console.log("2.2:", reason, reason.stack); | |
throw reason; | |
}); | |
p2.then(null, function(reason){ | |
console.log("2.3:", reason, reason.stack); | |
}); | |
// Cancelling p2 propagates to p1, which means that all promises derived | |
// from p1 get cancelled. | |
p2.cancel(); | |
} | |
function test3(){ | |
// Create a rejected promise p1 | |
var p1 = new legendary.Promise(function(){ | |
throw new Error("Resolver threw"); | |
}); | |
p1.then(function(value){ | |
console.log("3.1:", value); | |
}, function(reason){ | |
console.log("3.1:", reason, reason.stack); | |
throw reason; | |
}); | |
var p2 = p1.then(null, function(reason){ | |
console.log("3.2:", reason, reason.stack); | |
throw reason; | |
}); | |
p2.then(null, function(reason){ | |
console.log("3.3:", reason, reason.stack); | |
}); | |
// p1 is already rejected, so cancellation is a no-op. | |
p1.cancel(); | |
} | |
function test4(){ | |
// Create a rejected promise p1 | |
var p1 = new legendary.Promise(function(){ | |
throw new Error("Resolver threw"); | |
}); | |
p1.then(function(value){ | |
console.log("4.1:", value); | |
}, function(reason){ | |
console.log("4.1:", reason, reason.stack); | |
throw reason; | |
}); | |
// Even though p1 is rejected, p2 is pending because the callbacks don't | |
// execute until a next turn. | |
var p2 = p1.then(null, function(reason){ | |
console.log("4.2:", reason, reason.stack); | |
throw reason; | |
}); | |
p2.then(null, function(reason){ | |
console.log("4.3:", reason, reason.stack); | |
}); | |
// Cancelling p2 only cancels 4.2 and 4.3, but not 4.1. | |
p2.cancel(); | |
} | |
function test5(){ | |
// Create a resolved promise p1 that is adoping the state of a pending | |
// promise. | |
var p1 = new legendary.Promise(function(resolve){ | |
resolve(new legendary.Promise(function(resolve){ | |
setTimeout(resolve, 2000, "test5"); | |
})); | |
}); | |
p1.then(function(value){ | |
console.log("5.1:", value); | |
}, function(reason){ | |
console.log("5.1:", reason, reason.stack); | |
throw reason; | |
}); | |
var p2 = p1.then(null, function(reason){ | |
console.log("5.2:", reason, reason.stack); | |
throw reason; | |
}); | |
p2.then(null, function(reason){ | |
console.log("5.3:", reason, reason.stack); | |
}); | |
// Cancelling p1 cancels adoption, which means all promise derived from | |
// p1 get cancelled. | |
p1.cancel(); | |
} | |
function test6(){ | |
// Create a resolved promise p1 that is adoping the state of a pending | |
// promise. | |
var p1 = new legendary.Promise(function(resolve){ | |
resolve(new legendary.Promise(function(resolve){ | |
setTimeout(resolve, 2000, "test6"); | |
})); | |
}); | |
p1.then(function(value){ | |
console.log("6.1:", value); | |
}, function(reason){ | |
console.log("6.1:", reason, reason.stack); | |
throw reason; | |
}); | |
var p2 = p1.then(null, function(reason){ | |
console.log("6.2:", reason, reason.stack); | |
throw reason; | |
}); | |
p2.then(null, function(reason){ | |
console.log("6.3:", reason, reason.stack); | |
}); | |
// Cancelling p2 propagates to p1, which means that all promises derived | |
// from p1 get cancelled. | |
p2.cancel(); | |
} | |
function test7(){ | |
// Create a pending promise p1 with an onCancelled callback. | |
var p1 = new legendary.Promise(function(resolve){ | |
var id = setTimeout(resolve, 2000, "test7"); | |
this.onCancelled = function(){ | |
console.log("7.onCancelled"); | |
clearTimeout(id); | |
}; | |
}); | |
p1.then(function(value){ | |
console.log("7.1:", value); | |
}, function(reason){ | |
console.log("7.1:", reason, reason.stack); | |
throw reason; | |
}); | |
var p2 = p1.then(null, function(reason){ | |
console.log("7.2:", reason, reason.stack); | |
throw reason; | |
}); | |
p2.then(null, function(reason){ | |
console.log("7.3:", reason, reason.stack); | |
}); | |
// Cancelling p1 invokes the callback and means that all derived promises | |
// get cancelled too. | |
p1.cancel(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment