Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@parsingphase
Created February 5, 2015 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsingphase/a1f763e5e654d03789d0 to your computer and use it in GitHub Desktop.
Save parsingphase/a1f763e5e654d03789d0 to your computer and use it in GitHub Desktop.
Eternal hangup from promises.
/**
* Created by richardg on 05/02/2015.
*/
// ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
// a promise calls its passed-in resolve() function when it's done ?
//var Promise = require("nodegit-promise");
var Promise = require("promise");
//var fs = require('fs')
//var read = Promise.denodeify(fs.readFile)
//var ticker = setInterval(function () {
// console.log('.');
//}, 1000);
// this is valid, does nothing (minimal viable invocation of a Promise)
var p1 = new Promise(function (resolve, reject) {
});
console.info('p1 is a ' + typeof p1);
// resolve, reject functions are injected into the constructor argument function, to be called on resolution
var p2 = new Promise(function (resolve, reject) {
console.info('p2: resolve is a ' + typeof resolve);
console.info('p2: reject is a ' + typeof reject);
});
var p3 = new Promise(function (resolve, reject) {
resolve('p3 resolved');
});
console.info('p3 ' + ((p3 instanceof Promise) ? 'is' : 'is not') + ' a Promise');
var p3then = p3.then(function (param) {
console.info(param);
});
console.info('p3.then is a ' + typeof p3.then);
console.info('p3.then() returns a ' + typeof p3then);
console.info('p3.then() ' + ((p3then instanceof Promise) ? 'does' : 'does not') + ' return a Promise');
var p4 = new Promise(function (resolve, reject) {
setTimeout(function () {
resolve('p4 resolved');
}, 2500);
});
console.info('p4 ' + ((p4 instanceof Promise) ? 'is' : 'is not') + ' a Promise');
var p4then = p4.then(function (param) {
console.info(param);
//clearInterval(ticker);
});
console.info('p4.then is a ' + typeof p4.then);
console.info('p4.then() returns a ' + typeof p4then);
console.info('p4.then() ' + ((p4then instanceof Promise) ? 'does' : 'does not') + ' return a Promise');
var p5 = new Promise(function (resolve, reject) {
console.info('p5 created');
setTimeout(function () {
resolve('p5 resolved');
}, 200 + (Math.random() * 2500));
});
p5.then(function (msg) {
console.log(msg);
});
var p6 = new Promise(function (resolve, reject) {
setTimeout(function () {
reject('p6 failed');
}, 1000 + (Math.random() * 2500));
setTimeout(function () {
resolve('p6 ok');
}, (Math.random() * 2500));
});
p6.then(function (msg) {
console.info('p6 OK: ' + msg);
},
function (msg) {
console.info('p6 failed: ' + msg);
}
);
//p6.catch(function (msg) {
// console.info('Caught p6: ' + msg);
//});
//p6.fin(function (msg) { // fin, finally not implemented
// console.info('Finally-Caught p6: ' + msg);
//});
Promise.all([p4, p5]).done(function () {
console.info('Both p4, p5 ended');
//clearInterval(ticker);
});
//Promise.race([p4, p5]).then(function () {
// console.info('One of p4, p5 successful');
// //clearInterval(ticker);
//});
//Promise.all([p4, p5]).then(function () {
// console.info('Both p4, p5 successful');
// //clearInterval(ticker);
//});
Promise.race([p5, p6]).done(function () { // first one causes completion
console.info('One of p5, p6 ended');
//clearInterval(ticker);
});
//Promise.all([p5, p6]).done(function () { // both required
// console.info('Both p5, p6 ended');
// //clearInterval(ticker);
//});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment