Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "async-await-promises",
"version": "1.0.0",
"main": "src/index.js",
"scripts": {
"ex1": "tsc && node build/ex1.js",
"ex2": "tsc && node build/ex2.js",
"ex3": "tsc && node build/ex3.js",
"ex4": "tsc && node build/ex4.js",
"ex5": "tsc && node build/ex4.js",
"ex6": "tsc && node build/ex4.js"
},
"author": "Labenu",
"license": "ISC",
"dependencies": {
"axios": "^0.20.0"
},
"devDependencies": {
"@types/axios": "^0.14.0",
"typescript": "^4.0.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const baseURL: string = 'https://labenews.herokuapp.com';
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import axios from "axios"
import { baseURL } from "./baseURL"

// axios.get(`${baseURL}/subscribers`).then(console.log)

// Ex1-a)Qual endpoint você deve utilizar para isso?
// RES: Metodo Get


// Ex1-b)Como indicamos o tipo de uma função assíncrona que retorna um "array de qualquer coisa"?
// RES: ":Promise <any[]>"

// Ex1-c)
async function getSubscribers (): Promise<any[]> {
const response = await axios.get(`${baseURL}/subscribers`);
return response.data;
console.log(response.data)
}
console.log(getSubscribers())

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import axios from "axios"
import { baseURL } from "./baseURL"

//Ex-1-a)
// Amsior diferença entre as sintaxes é o inicio na função normal ela começa com async function nome_da_funcao ():Promise<any[]> {}
// já na arrow function ele começa com const Nome_da-funçao = async

//Ex-1-b)
const getSubscribers = async (): Promise<any[]> => {
const response = await axios.get(`${baseURL}/subscribers`);
return response.data;
};

console.log(getSubscribers())


Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import axios from "axios"
import { baseURL } from "./baseURL"

//Ex-3-a)
//RES: funcionaPerfeitamente

type user = {
id: string;
name: string;
email: string;
}

//Ex-3-b)

//Ex-3-c)

const getSubscribers = async (): Promise<user[]> => {
const response = await axios.get(`${baseURL}/subscribers`);
return response.data.map((res: any) => {
return {
id: res.id,
name: res.name,
email: res.email,
};
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import axios from "axios"
import { baseURL } from "./baseURL"


//Ex-4-a)
//RES: metodo put, pq diz na documentação

//Ex-4-b)
const createNews = async(
title: string,
content: string,
date: number
): Promise<void> => {
await axios.put(`${baseURL}/news`, {
title: title,
content: content,
date: date
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import axios from "axios"
import { baseURL } from "./baseURL"

//Ex5)


// const sendNotifications = async (
// users: user[],
// message: string
// ): Promise<void> => {

// try {
// for (const user of users) {
// await axios.post(`${baseURL}/notifications`, {
// subscriberId: user.id,
// message
// });
// }

// console.log("All notifications sent");
// } catch {
// console.log("Error");
// }
// };
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import axios from "axios"
import { baseURL } from "./baseURL"

//Ex-6)


// const sendNotifications = async (
// users: user[],
// message: string
// ): Promise<void> => {

// try {
// const promises = users.map(user =>{
// return axios.post(`${baseURL}/notifications`, {
// subscriberId: user.id,
// message: message,
// })
// })

// await Promise.all(promises);

// } catch {
// console.log("Error");
// }
// };
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}