Skip to content

Instantly share code, notes, and snippets.

@deconstructionalism
Created June 4, 2019 16:35
Show Gist options
  • Save deconstructionalism/3d561ccf52e1c8cf15d0fdcee74a8e82 to your computer and use it in GitHub Desktop.
Save deconstructionalism/3d561ccf52e1c8cf15d0fdcee74a8e82 to your computer and use it in GitHub Desktop.
// E X A M P L E 1 (backend specific)
// Objection chaining, using promises
const getDog = (id) => Dog.query()
.findById(id)
const getDog = (id) => {
return Dog.query()
.finById(id)
}
getDog(4)
.then(data => res.json(data))
.catch(console.error)
const createDog = (data) => getPetTypeIdFromType('dog')
.then(id => _createPet(data, Dog, id))
const createDog = async (data) => {
const petTypeId = await PetType.query()
.findOne({ type })
.then(data => data.id)
return _createPet(data, Dog, petTypeId)
}
myPromise()
.then(promiseChainReturningFunction)
.then(console.log)
Promise.all([ axios.get(url1), axios.get(url2), axios.get(url3) ])
.then(console.log)
.catch()
/*
[
{ data: 'has dogs' },
{ data: 'ok then' },
{ data: 'bye' },
]
*/
const getFromUrls = [url1, url2, url3]
Promise.all( getFromUrls.map(url => axios.get(url)) )
.then(console.log)
const parseDataAndRequest = async (data) => {
const { urls } = data
if (!urls) {
throw new Error('no urls in data')
}
// const petTypeId = await PetType.query()
// .findOne({ type })
// .then(data => data.id)
const responses = await Promise.all( urls.map(async url => {
await axios.post('http://myurl.com/logger', {
body: {
message: `posted to URL ${url}`
}
})
return axios.get(url)
}))
return responses
}
// E X A M P L E 2 (generic)
// using a promise
axios.get(url)
.then(console.log)
.catch(console.error)
// writing a promise
const get = (url) => {
return new Promise((resolve, reject) => {
// const data = XMLHttpRequest(url)....
if(data.error) {
reject(data)
}
resolve(data)
})
}
// using async await instead of promise chaining
const getUrlData = async () => {
try {
const data = await axios.get(url)
console.log(data)
} catch(err) {
console.error(err)
}
}
const getAndExtractUrlData = async () => {
try {
const data = await axios.get(url)
.then(data => doAsyncExtraction(data))
console.log(data)
} catch(err) {
console.error(err)
}
}
const getAndExtractUrlData = async () => {
try {
const data = await axios.get(url)
const extractedData = await doAsyncExtraction(data)
console.log(extractedData)
} catch(err) {
console.error(err)
}
}
// Promise.all takes an array of Promises as it's argument
Promise.all([ axios.get(url1), axios.get(url2), axios.get(url2) ])
.then(console.log)
.catch(console.error)
/*
[
{ data: 'something },
{ data: 'else },
{ data: 'here },
]
*/
const urls = [
// a bunch of urls to get from
// indeterminate length
]
Promise.all( urls.map(url => axios.get(url)) )
.then(console.log)
.catch(console.error)
const getDataFromUrls = async (urls) => {
const data = await Promise.all( urls.map(url => axios.get(url)) )
console.log(data)
}
const getDataFromUrlsAndLog = async (urls) => {
const data = await Promise.all( urls.map(async url => {
await axios.post('http://mylogger.url/log', {
data: {
message: `attempting to get url ${url}`
}
})
return axios.get(url)
}))
console.log(data)
}
/*
for each url
log that you are getting that url
get that url and return the data
log all the data
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment