Skip to content

Instantly share code, notes, and snippets.

@KDCinfo
Forked from mpj/example01.js
Last active February 8, 2018 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KDCinfo/c816c9cf52e55d059d98cb39538e90d4 to your computer and use it in GitHub Desktop.
Save KDCinfo/c816c9cf52e55d059d98cb39538e90d4 to your computer and use it in GitHub Desktop.
Code for the async/await episode of Fun Fun Function from https://www.youtube.com/watch?v=568g8hxJJp4
const response = await fetch(`https://catappapi.herokuapp.com/users/${userId}`)
const data = await response.json()
return data.imageUrl
}
const response = await fetch(`https://catappapi.herokuapp.com/users/${userId}`)
const data = await response.json()
return data.imageUrl
}
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)
})
}
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
}
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) {
// Per a comment on original Gist: using `await` provides a value; not a Promise.
// Either can work; just know what you're expecting.
return 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
}))
}
// Original Gist (forked from): https://gist.github.com/mpj/3f8bc0c6ecda4294fbeff99f1e3fae85
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) {
// Per a comment on original Gist: using `await` provides a value; not a Promise.
// Either can work; just know what you're expecting.
return 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
}))
}
// Original Gist (forked from): https://gist.github.com/mpj/3f8bc0c6ecda4294fbeff99f1e3fae85
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)
}
}
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()))
}
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)
})
@KDCinfo
Copy link
Author

KDCinfo commented Feb 8, 2018

Thanks to: Fun Fun Function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment