이머시브 코스 TIL 4주차 모음
const printString = (string, callback) => {
setTimeout(
() => {
console.log(string)
callback()
},
Math.floor(Math.random() * 100) + 1
)
}
const printAll = () => {
printString("A", () => {
printString("B", () => {
printString("C", () => {})
})
})
}
printAll()
.then()
/ .catch()
resolve()
: 기능을 정상적으로 수행해서 최종 데이터를 전달하는 콜백함수reject()
: 기능을 수행하다가 문제가 생겼을 때 호출하게 될 함수const printString = (string) => {
return new Promise((resolve, reject) => {
setTimeout(
() => {
console.log(string)
resolve()
},
Math.floor(Math.random() * 100) + 1
)
})
}
const printAll = () => {
printString("A")
.then(() => {
return printString("B")
})
.then(() => {
return printString("C")
})
}
printAll()
function gotoCodestates(){
return new Promise((resolve, reject) => {
setTimeout(() => {resolve('1. go to codestates')}, 100)
})
}
function sitAndCode(){
return new Promise((resolve, reject) => {
setTimeout(() => {resolve('2. sit and code')}, 100)
})
}
function eatLunch(){
return new Promise((resolve, reject) => {
setTimeout(() => {resolve('3. eat lunch')}, 100)
})
}
function gotoBed(){
return new Promise((resolve, reject) => {
setTimeout(() => {resolve('4. go to bed')}, 100)
})
}
const result = async() => {
const one = await gotoCodestates()
console.log(one)
const two = await sitAndCode()
console.log(two)
const three = await eatLunch()
console.log(three)
const four = await gotoBed()
console.log(four)
}
result()
require
구문을 이용하여 모듈 불러오기const fs = require('fs') // 파일 시스템 모듈을 불러옵니다
const dns = require('dns') // DNS 모듈을 불러옵니다
fs.readFile
로 로컬파일 읽어오기fs.readFile('test.txt', 'utf8', (err, data) => {
if (err) {
throw err; // 에러를 던집니다.
}
console.log(data);
});
fetch(url)
.then(response => response.json()) // 자체적으로 json() 메소드가 있어, 응답을 JSON 형태로 변환시켜서 다음 Promise로 전달합니다
.then(json => console.log(json)) // 콘솔에 json을 출력합니다
.catch(error => console.log(error)); // 에러가 발생한 경우, 에러를 띄웁니다
OSI 7계층(응용계층)
let message = {
username: '김코딩',
text: '새 글을 써보겠습니다',
roomname: '사랑방'
};
const serverURL = 'http://3.36.72.17:3000/kimcoding/messages';
fetch(serverURL, {
method: 'POST',
body: JSON.stringify(message), // stringify 과정이 반드시 필요합니다. 왜일까요?
headers: {
'Content-Type': 'application/json'
},
})
.then(response => response.json())
.then(json => {
console.log(json)
console.log('새 글을 작성했습니다')
});
const fs = require('fs')
const http = require('http')
fs.readFile('./something.json', (err, data) => {
console.log(data)
})
http.get('http://localhost:5000/api', (ref) => {
console.log(res)
})
yarn add @babel/core —dev
npm install @babel/core —save-dev
—dev 옵션을 줘서 등록yarn add react
npm install —save react
npm을 쓸때는 —save 옵션을 줘야함npm init
을 해주면 package.jsonnpm install --save nodemon
nodemon index.js
index.js가 변경되는대로 바로 반영해서 실행
const defaultCorsHeader = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Accept',
'Access-Control-Max-Age': 10
};