Created
August 14, 2017 07:38
-
-
Save mpj/3f8bc0c6ecda4294fbeff99f1e3fae85 to your computer and use it in GitHub Desktop.
Code for the async/await episode of Fun Fun Function.
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
const response = await fetch(`https://catappapi.herokuapp.com/users/${userId}`) | |
const data = await response.json() | |
return data.imageUrl | |
} |
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
const response = await fetch(`https://catappapi.herokuapp.com/users/${userId}`) | |
const data = await response.json() | |
return data.imageUrl | |
} |
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
function fetchCatAvatars(userId) { | |
return fetch(`https://catappapi.herokuapp.com/users/${userId}`) | |
.then(response => response.json()) | |
.then(user => { | |
const promises = user.cats.map(catId => | |
fetch(`https://catappapi.herokuapp.com/cats/${catId}`) | |
.then(response => response.json()) | |
.then(catData => catData.imageUrl)) | |
return Promise.all(promises) | |
}) | |
} |
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
async function fetchCatAvatars(userId) { | |
const response = await fetch(`http://catappapi.herokuapp.com/users/${userId}`) | |
const user = await response.json() | |
const catImageUrls = [] | |
for (const catId of user.cats) { | |
const response = await fetch(`http://catappapi.herokuapp.com/cats/${catId}`) | |
const catData = await response.json() | |
catImageUrls.push(catData.imageUrl) | |
} | |
return catImageUrls | |
} |
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
async function fetchCatAvatars(userId) { | |
const response = await fetch(`http://catappapi.herokuapp.com/users/${userId}`) | |
const user = await response.json() | |
return await Promise.all(user.cats.map(async function(catId) { | |
const response = await fetch(`http://catappapi.herokuapp.com/cats/${catId}`) | |
const catData = await response.json() | |
return catData.imageUrl | |
})) | |
} |
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
async function fetchCatAvatars(userId) { | |
const response = await fetch(`http://catappapi.herokuapp.com/users/${userId}`) | |
const user = await response.json() | |
return await Promise.all(user.cats.map(async function(catId) { | |
const response = await fetch(`http://catappapi.herokuapp.com/cats/${catId}`) | |
const catData = await response.json() | |
return catData.imageUrl | |
})) | |
} |
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
const processUser = require('imaginary_service/process_user') | |
async function processAllUsers () { | |
const sql = 'SELECT id FROM users' | |
const users = await db.query(sql, []) | |
for (const user of users) { | |
await processUser(user.id) | |
} | |
} |
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
const processUser = require('imaginary_service/process_user') | |
function processAllUsers () { | |
const sql = 'SELECT id FROM users' | |
return db.query(sql, []) | |
.then(users => | |
users.reduce((lastPromise, user) => | |
lastPromise.then(_ => processUser(user.id)) | |
, Promise.resolve())) | |
} |
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 restify = require('restify') | |
const corsMiddleware = require('restify-cors-middleware') | |
const db = { | |
users: { | |
'123': { | |
name: 'mpj', | |
cats: [ 21, 33, 45 ], | |
imageUrl: 'http://images.somecdn.com/user-123.jpg' | |
} | |
}, | |
cats: { | |
'21': { | |
name: 'Fluffykins', | |
imageUrl: 'http://images.somecdn.com/cat-21.jpg', | |
}, | |
'33': { | |
name: 'Waffles', | |
imageUrl: 'http://images.somecdn.com/cat-33.jpg' | |
}, | |
'45': { | |
name: 'Sniffles', | |
imageUrl: 'http://images.somecdn.com/cat-45.jpg' | |
} | |
} | |
} | |
function cats(req, res, next) { | |
res.send(db.cats[req.params.id]) | |
next() | |
} | |
function users(req, res, next) { | |
res.send(db.users[req.params.id]) | |
next() | |
} | |
var server = restify.createServer() | |
const cors = corsMiddleware({ | |
origins: ['*'] | |
}) | |
server.pre(cors.preflight) | |
server.use(cors.actual) | |
server.get('/cats/:id', cats) | |
server.get('/users/:id', users) | |
server.listen(process.env.PORT || 8080, function() { | |
console.log('%s listening at %s', server.name, server.url) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any reason why example01.js and example02.js are the same ?