Created
June 15, 2020 14:17
-
-
Save GKephart/4bddd3f871923881a22fa9f2e3fb1289 to your computer and use it in GitHub Desktop.
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
import axios from "axios" | |
interface Post { | |
postId: string | null, | |
postUserId: number, | |
postContent: string, | |
postTitle: string | |
} | |
function dataDownloader() : Promise<any> { | |
return main() | |
async function main() { | |
try { | |
await downloadPosts() | |
} catch (e) { | |
console.log(e) | |
} | |
} | |
async function downloadPosts() { | |
try { | |
const {data} = await axios.get("https://jsonplaceholder.typicode.com/posts") | |
const createPosts = (array: any[]) : Post[] => { | |
//instead of putting the posts into an array insert them into the database. | |
const posts : Post[] = [] | |
for(let currentPost of array) { | |
let post : Post = {postId: null, postUserId: currentPost.userId, postContent: currentPost.body, postTitle: currentPost.title} | |
posts.push(post) | |
} | |
return posts | |
} | |
console.log(createPosts(data)) | |
} catch (error) { | |
console.error(error) | |
} | |
} | |
} | |
dataDownloader().catch(error => console.error(error)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment