자바스크립트 callback

동기와 비동기

'use script'
// JavaScript is synchronous.
// 자바스크립트는 동기적인 프로그래밍 언어이다.
// hoisting: var, function dectaration 
// hoisting: 함수 안에 있는 선언들을 모두 끌어올려서 해당 함수 유효 범위의 최상단에 선언하는 것

console.log('1');
setTimeout(()=>console.log('2'),1000); // 브라우저 요청 -> 1초 후 실행 
console.log('3');   
// 1 3 2 출력
// => 비동기적인 실행 방법

//Synchronous callback (동기) 
// 함수의 선언은 호이스팅 되기때문에 제일 위로 가게됨 
function printImmediately(print){
    print();
}
 printImmediately(()=> console.log('hello'));
 
 
// Asynchronous callback (비동기)
// 함수의 선언은 호이스팅 되기때문에 제일 위로 가게됨 
function printWithDelay(print, timeout){
  setTimeout(print, timeout);
}
printWithDelay(()=> cosole.log('Async CallBack'),2000); // 제일 마지막에 출력

콜백지옥체험 😱

Last updated

Was this helpful?