This file contains hidden or 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
| // 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) | |
| } |
This file contains hidden or 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
| const join = (fn, ...params) => | |
| Promise | |
| .all(params) | |
| .then(ps => fn.apply(null, ps)); |
This file contains hidden or 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
| const Promise = require('bluebird') | |
| const makeRequest = () => { | |
| const p1 = promise1() | |
| const p2 = p1.then(promise2) | |
| return Promise | |
| .join(p1,p2) | |
| .then(promise3) | |
| } |
This file contains hidden or 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
| 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()); | |
| } | |
This file contains hidden or 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
| 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; |
This file contains hidden or 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
| 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()) | |
| ) | |
| ); |
This file contains hidden or 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
| /** 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)); |
This file contains hidden or 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
| 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'], { |
This file contains hidden or 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 friends = await people.map(person => await person.getFriends()); |
This file contains hidden or 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
| /** 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')); |