-
-
Save sgimeno/9785739 to your computer and use it in GitHub Desktop.
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.userdata = c.userdata.index.href; | |
return c; | |
}); | |
} | |
function parallel() { | |
var promises = arguments; | |
return function(context){ | |
return Q.fcall(function(){ | |
return { promise: Q.all(promises), ctx: context}; | |
}); | |
} | |
} | |
function combine(requests){ | |
var context = requests.ctx; | |
var aggregate = function(resolutions){ | |
for (var i in resolutions) | |
for (var key in resolutions[i]) | |
context[key] = resolutions[i][key]; | |
return context; | |
}; | |
return requests.promise | |
.then(aggregate); | |
} | |
Q.fcall(login.bind(null, 'lolo')) | |
.then(parallel(getStores(), getUserData())) | |
.then(combine) | |
.then(console.log); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment