λκΈ°μ λΉλκΈ°
Syncronous & Asychronous
1. μΌλ°μ μΈ λ§₯λ½μμμ λκΈ°(Sync)
μμμ 보λ€κ° μλ§μ΄ μμλ³΄λ€ λΉ λ₯΄κ±°λ λ¦κ² μΆλ ₯λλ κ²½μ°, 'μ±ν¬(Sync)κ° μ λ§λλ€' λΌλ λ§μ μ¬μ©νλ€. μ΄λ λΉλκΈ°(Asychronous)λ₯Ό μλ―Ένλ€.
μ΄λμ μ±ν¬λ Syncλ₯Ό μλ―Ένλλ°, sysnchronousμ μ€μλ§λ‘μ, 'λκΈ°'νΉμ 'λμμ μΌμ΄λλ, ν κ°μ§ λ, κΈ°λ€λ¦΄ κΈ°)' μ΄λΌλ μλ―Έλ₯Ό κ°μ§κ³ μλ€.
2. νλ‘κ·Έλλ°μμμ λκΈ°(Sync)μ λΉλκΈ°(Async)
λκΈ°, Synchronous
μ΄μ μμ μ΄ λ€ μνλμ§ μμ κ²½μ°, λ€μ μμ μ μνλμ§ μμ κ²½μ°, λ€μ μμ μ μννμ§ λͺ»νλ μνκ° λλ€.
μ΄λ₯Ό (Blocking) μ΄λΌκ³ νλ€.
ex) λ‘κ·ΈμΈ λ²νΌ ν΄λ¦ μ νλ©΄μ΄ μλ‘κ³ μΉ¨ λλ©΄μ λ‘κ·ΈμΈ νλ©΄μΌλ‘ μ ν λμ΄μΌ ν λ λ‘κ·ΈμΈ νλ©΄μΌλ‘ μ΄λνλ λμ μ¬μ©μλ ν΄λΉ νλ©΄μ΄ λμ¬ λ κΉμ§ λ€λ₯Έ λμμ ν μ μκ² λλ€.
λΉλκΈ°, Asynchronous
μ΄μ μμ μ μν μ¬λΆμ μκ΄μμ΄ λ€μ μμ μ κ°λ³λ‘ μν(Non-Blocking)
λΉλκΈ° λ°©μμ μ²λ¦¬ μμκ° λ³΄μ₯λμ§ μλ λμ λκΈ° λ°©μμ λΉν΄ κ²°κ³Ό νλ©΄μ΄ λμ€λ μκ° λμ λ€λ₯Έ μμ μ ν μ μλ€λ μ₯μ μ΄ μλ€.
// μ½λλ‘ λ³΄λ λκΈ° λΉλκΈ°
λκΈ°μ λΉλκΈ°μ
νμ΄μ§λ₯Ό μλ‘κ³ μΉ¨νλ μ νλμ λ°λΌ λλλ€,
call stack, task queue μ΄λ task queueλ λΉλκΈ°μ μΌλ‘ μλν λ μλ₯Όλ€μ΄ APIμΈ setTimeout μμ first ν¨μκ° μλ€λ©΄ callbackμμ λ°λ‘ task queueλ‘ λ€μ΄κ°λ€.
ex) function first() { console.log('first() called'); }
function second() { setTimeout(() => console.log('second() called'), 1000); }
function third() { console.log('third() called'); }
first();
second();
third();
//
//νμ¬ μ€ν μ€μΈ μμ
μ΄ μμ§ μ’
λ£λμ§ μμ μνλΌκ³ ν΄λ, λ€μ μμ
μ κ³§λ°λ‘ μνν μ μλ λ°©μ
function first() {
console.log('first() called');
}
function second() {
console.log('second() called');
}
setTimeout(first, 3 * 1000);
second();
//
function first() {
console.log('first() called');
}
function someLongWork() {
console.log('μ€λ 걸리λ μμ
μν μ€...');
}
function second() {
console.log('second() called');
}
first();
someLongWork();
second();
μ€μ΅ λ§ν¬