목록전체 글 (796)
코드 그라데이션
자바스크립트를 실행하는 와중에 Node.js의 모듈을 이용하여 js 파일을 다른 곳에서 실행할 수 있는 기능이 있었다. index.js const calc = require("./calc"); // 내장함수, 경로 console.log(calc); // 모듈을 출력 console.log(calc.add(1, 2)); console.log(calc.add(4, 5)); console.log(calc.sub(10, 2)); calc.js const add = (a, b) => a+b; const sub = (a, b) => a+b; module.exports = { moduleName : "calc module", add : add, sub : sub, }; 실행 결과
API란? 예시 let response = fetch("https://jsonplaceholder.typicode.com/posts").then((res) => { console.log(res); }) 조금 더 세련되게 json 형식으로 가져와보겠다. async function getData() { let rawResponse = await fetch("https://jsonplaceholder.typicode.com/posts"); let jsonResponse = await rawResponse.json(); // json 형태로 반환 console.log(jsonResponse); } getData();
async // async function hello () { return "hello"; } async function helloAsync() { return "hello Async"; } console.log(hello()); console.log(helloAsync()); // async를 붙여주면 자동적으로 비동기 함수가 된다. 이말은 then을 쓸 수 있다는 것인데, async 붙은 함수의 리턴값은, 비동기 작업 객체 Promise의 resolve의 결과값이 된다. 그러니까 async를 붙이고 그냥 리턴만 해도, promise를 리턴하면서 resolve를 이 return 값으로 수행한 것과 똑같은 결과를 얻는다고 생각하면 된다. await 기다렸다가 끝나는 함수를 만들자 일단 async 응용 f..