Created
March 26, 2014 15:13
-
-
Save joseraya/9785650 to your computer and use it in GitHub Desktop.
promises fun
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 Q = require('q'); | |
function printNumber(n) { | |
return Q.fcall(function () { | |
console.log("Number: ", n); | |
return n; | |
}); | |
} | |
function printNumbers(numbers) { | |
if (numbers.length === 0) { | |
return; | |
} else { | |
var current = numbers.pop(); | |
console.log(current, numbers); | |
return printNumber(current).then(function () { | |
printNumbers(numbers); | |
}); | |
} | |
} | |
Q.all(printNumber(1), printNumber(2), printNumber(3)).then(function () { | |
console.log(arguments); | |
}); |
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 Q = require('q'); | |
function printNumber(n) { | |
return Q.fcall(function () { | |
console.log("Number: ", n); | |
if (n % 3 === 0) { | |
return Q.reject(n); | |
} else { | |
return n; | |
} | |
}); | |
} | |
function printNumbers(numbers, errors) { | |
if (numbers.length === 0) { | |
return errors; | |
} else { | |
var current = numbers.pop(); | |
return printNumber(current).then(function () { | |
return printNumbers(numbers, errors); | |
}, function (rejection) { | |
errors.push(rejection); | |
return printNumbers(numbers, errors); | |
}); | |
} | |
} | |
Q.fcall(function () { | |
return printNumbers([1, 2, 3, 4, 5, 6], []); | |
}).then(function () { | |
console.log(arguments); | |
}); |
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 Q = require('q'); | |
function getIndex() { | |
var context = {}; | |
context.url = 'http://www.agilogy.com'; | |
return context; | |
} | |
function getSomething(context) { | |
console.log("URL IS", context.url); | |
context.something = "Something"; | |
return context; | |
} | |
Q.fcall(getIndex) | |
.then(getSomething) | |
.then(console.log); |
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 Q = require('q'); | |
function emptyContext(c) { | |
return Q.fcall(function () { | |
return c || {}; | |
}); | |
} | |
function login(username) { | |
return { | |
token: 'username' | |
}; | |
} | |
function getIndex(uri, param) { | |
return function (context) { | |
context[param] = context[param] || {}; | |
context[param].index = { | |
href: uri + '/index' | |
}; | |
return context; | |
}; | |
} | |
function getStores(context) { | |
return emptyContext(context) | |
.then(getIndex('http://stores.com', 'stores')) | |
.then(function (c) { | |
c.stores.data = {}; | |
c.stores.data.stores = c.stores.index.href; | |
return c; | |
}); | |
} | |
function getUserData(context) { | |
return emptyContext(context) | |
.then(getIndex('http://userdata.com', 'userdata')) | |
.then(function (c) { | |
c.userdata.data = {}; | |
c.userdata.data.stores = c.userdata.index.href; | |
return c; | |
}); | |
} | |
function combine(context) { | |
return Q.all(arguments).then(function (results) { | |
/*for (var i in results) { | |
context[i] = results[i]; | |
}*/ | |
context.a = 1; | |
//console.log("context", context); | |
return context; | |
}); | |
} | |
Q.fcall(login.bind(null, 'lolo')) | |
.then(combine(getStores(), getUserData())) | |
.then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment