Skip to content

Instantly share code, notes, and snippets.

View ktilcu's full-sized avatar

kyle tilman ktilcu

View GitHub Profile
@ktilcu
ktilcu / AA4-example.js
Last active January 4, 2018 18:40
Don't skip Promises for A/A
// Use the result of one promise to call another and then that result and the first to call a third
const join = (fn, ...params) => Promise.all(params).then(ps => fn.apply(null, ps));
const makeRequest = () => {
const p1 = promise1()
const p2 = p1.then(promise2)
return join(promise3, p1, p2)
}
@ktilcu
ktilcu / AA4-join.js
Last active January 4, 2018 18:44
Promise.join - simply
const join = (fn, ...params) =>
Promise
.all(params)
.then(ps => fn.apply(null, ps));
const Promise = require('bluebird')
const makeRequest = () => {
const p1 = promise1()
const p2 = p1.then(promise2)
return Promise
.join(p1,p2)
.then(promise3)
}
const getAvatars = async (userId) => {
const user = await getUser(userId);
const avatar = await user.avatar();
const friends = await user.friends();
const avatars = [avatar];
for (let friend of friends) {
avatars.push(await friend.avatar());
}
const getAvatars = async (userId) => {
const user = await getUser(userId);
const [avatar, friends] = await Promise.all([user.avatar(), user.friends()])
const avatars = [avatar];
for (let friend of friends) {
avatars.push(await friend.avatar());
}
return avatars;
const getAvatars = async (userId) => {
const user = await getUser(userId);
const [avatar, friends] = await Promise.all([user.avatar(), user.friends()])
const avatars = [avatar];
return avatars.concat(
await Promise.all(
friends.map(friend => friend.avatar())
)
);
/** Promise Utility Functions **/
// Left to right function composition
const pipe = ([...asyncFns]) => input =>
asyncFns.reduce((a, f) => a.then(f), Promise.resolve(input));
// One input passed to many processors simultaneously
const fan = ([...fns]) => value => Promise.all(fns.map(fn => fn(value)));
// Map a list with an async function and wait for all promises to resolve
const map = fn => list => Promise.all(list.map(fn));
@ktilcu
ktilcu / ahh.js
Last active January 17, 2018 21:21
imfearless
const get = key => obj => obj[key];
const flatten = list => list.reduce((a, b) => a.concat(b), []);
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
function queryAPI(region, realm, character) {
if (region == null && realm == null && character == null) {
return; // new Error or whatever
}
return blizzard.wow
.character(['profile', 'items', 'talents', 'progression'], {
@ktilcu
ktilcu / aa-when.js
Created January 18, 2018 14:20
The hidden problem with async/await
var friends = await people.map(person => await person.getFriends());
@ktilcu
ktilcu / AA4-promises.js
Last active January 18, 2018 19:51
Alternative Promise code
/** Utility Functions **/
// Calls a method on an object
const call = method => value => value[method]();
/** Business Logic **/
const getUserAndFriendsAvatars = async user => {
// get user
const user = getUser(user);
// get users friends
const friends = user.then(call('friends'));