Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4cf1b13

Browse files
added folder: generators vs functions erickwendel
1 parent 9f99ad8 commit 4cf1b13

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
-> enriquecimento de dados!
3+
4+
1. le de um banco
5+
2. bater em uma API para pegar o resto das informacoes
6+
3. submeter os dados para outra API
7+
*/
8+
import axios from 'axios'
9+
const myDB = async () => Array.from({ length: 1000}, (v, index) => `${index}-cellphone`)
10+
11+
const PRODUCTS_URL = 'http://localhost:3000/products'
12+
const CART_URL = 'http://localhost:4000/cart'
13+
14+
async function processDbData() {
15+
const products = await myDB()
16+
const responses = []
17+
for(const product of products) {
18+
const { data: productInfo } = await axios.get(`${PRODUCTS_URL}?productName=${product}`)
19+
const { data: cartData } = await axios.post(`${CART_URL}`, productInfo)
20+
responses.push(cartData)
21+
}
22+
23+
return responses
24+
}
25+
26+
// console.table(await processDbData())
27+
28+
async function* processDbDataGen() {
29+
const products = await myDB()
30+
31+
for(const product of products) {
32+
const { data: productInfo } = await axios.get(`${PRODUCTS_URL}?productName=${product}`)
33+
const { data: cartData } = await axios.post(`${CART_URL}`, productInfo)
34+
yield cartData
35+
}
36+
}
37+
38+
for await(const data of processDbDataGen()) {
39+
console.table(data)
40+
}

‎Generators vs Functions - ErickWendel YouTube/package-lock.json

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "data-integration",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "erickwendel",
12+
"license": "ISC",
13+
"dependencies": {
14+
"axios": "^0.24.0"
15+
}
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// curl "localhost:3000/products?productName=sabao"
2+
3+
import { createServer } from 'http'
4+
import { parse } from 'url'
5+
import { randomUUID } from 'crypto'
6+
const PORT = 3000
7+
async function handler(request, response) {
8+
if (
9+
request.method === 'GET' &&
10+
request.url.includes('products')
11+
) {
12+
const { query: { productName } } = parse(request.url, true)
13+
const result = {
14+
id: randomUUID(),
15+
product: productName
16+
}
17+
18+
return response.end(JSON.stringify(result))
19+
}
20+
21+
22+
return response.end('hey!')
23+
}
24+
25+
createServer(handler)
26+
.listen(PORT, () => console.log(`products API is running at ${PORT}`))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// curl -X POST "localhost:4000/cart" --data '{"id": "123"}'
2+
3+
import { createServer } from 'http'
4+
5+
const PORT = 4000
6+
async function handler(request, response) {
7+
if (
8+
request.method === 'POST' &&
9+
request.url.includes('cart')
10+
) {
11+
for await (const data of request) {
12+
const item = JSON.parse(data)
13+
console.log('received', item)
14+
15+
return response.end(`process succeeded for ${item.id}`)
16+
}
17+
}
18+
19+
20+
return response.end('hey!')
21+
}
22+
23+
createServer(handler)
24+
.listen(PORT, () => console.log(`cart API is running at ${PORT}`))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /