๐Ÿ’ช JavaScript. Synchronous, asynchronous w/ ์ƒํ™œ์ฝ”๋”ฉ

[meษช]ยท2022๋…„ 2์›” 17์ผ
0

1-2. Today I Learned. JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
6/8

Preface

๐Ÿ“Œ ๊ฐœ๋ฐœ ๊ณต๋ถ€ 6๊ฐœ์›” ์ฐจ์ธ to-be ๊ฐœ๋ฐœ์ž์˜ ์ž์Šต blog๐Ÿ™‚๏พ Feb 13 ~ 19, 2021


ํ˜„์žฌ ์ƒํƒœ
๋ณต์Šตํ•˜๋ผ๋Š” ๋ง ๋งŽ์ด ๋“ค์—ˆ๋˜ ๋™๊ธฐ, ๋น„๋™๊ธฐ ๊ฐ•์˜ ํ•™์Šต
(+) ๋งˆ์นจ ๊ด€๋ จ ๊ฐ•์˜๋ฅผ ์˜ฌ๋ ค์ฃผ์‹  ์ด๊ณ ์ž‰๋‹˜!





1. callback function

callback ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ ๊ฐ’์œผ๋กœ ์ „๋‹ฌ๋˜์–ด์„œ ๋‚˜์ค‘์— ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜

// ๐ŸŽต 6๊ธ€์ž๋ณด๋‹ค ๋งŽ์€ ๋‹จ์–ด ์ถ”์ถœํ•˜๊ธฐ
words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

// (1)
newWords = words.filter(element => element.length > 6); // ์ต๋ช…ํ•จ์ˆ˜
console.log(newWords);
// โฌ‡๏ธ Output
// ['exuberant', 'destruction', 'present']

// (2)
function newfilter(origin, callback) {
  var result = [];
  for (var i = 0 ; i < origin.length ; i++) {
    var current = origin[i];
    if (callback(current)) {
      result.push(current);
    }
  }
  return result;
}
newWords = newfilter(words, element => element.length > 6);
console.log(newWords);
// โฌ‡๏ธ Output
// ['exuberant', 'destruction', 'present']

โœ”๏ธ first class citizen (1๊ธ‰ ์‹œ๋ฏผ / ๊ฐ์ฒด)
๋ณ€์ˆ˜๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋ฉด, ๋ณ€์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ return ๊ฐ’์ด ๋  ์ˆ˜ ์žˆ์œผ๋ฉด, ๋ณ€์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ ๊ฐ’์ด ๋  ์ˆ˜ ์žˆ์œผ๋ฉด 1๊ธ‰ ์‹œ๋ฏผ
์•„๋‹ˆ๋ฉด, ์—†์œผ๋ฉด 2๊ธ‰ ์‹œ๋ฏผ


2. Promise

  • "๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค."
    • ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฏ€๋กœ ์ด์ „ code์˜ ์‹คํ–‰์ด ์™„๋ฃŒ๋˜๊ธฐ ์ „๊นŒ์ง€ ๋‹ค์Œ code๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š์Œ
    • ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์‰ฌ์šฐ๋‚˜ ์†๋„๊ฐ€ ๋Š๋ฆด ์ˆ˜ ์žˆ์Œ
  • "๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค."
    • ๋…๋ฆฝ์ ์œผ๋กœ, ์ž์‹ ์˜ ์‹œ๊ฐ„ํ‘œ์— ๋งž๊ฒŒ, ๋ณ‘๋ ฌ์ ์œผ๋กœ ์‹คํ–‰๋จ (์ด์ „ code์˜ ์‹คํ–‰์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š์Œ)
    • ์ˆœ์„œ๋Š” ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ์œผ๋‚˜ ์†๋„๊ฐ€ ๋น ๋ฆ„
    • ์˜ˆ๋ฅผ ๋“ค์–ด, server์™€ web browser๊ฐ€ ํ†ต์‹ ํ•˜๋Š” ์ƒํ™ฉ์—์„œ ์–ธ์ œ ๋๋‚ ์ง€ ๋ชจ๋ฅด๋Š” ํ†ต์‹ ์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ํ†ต์‹ ํ•˜๋Š” ๋™์•ˆ ๋‹ค๋ฅธ ์ผ์„ ํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€ ํ†ต์‹ ์ด ๋๋‚˜๋ฉด callback ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด์„œ ํ•„์š”ํ•œ ์ž‘์—…์„ ๊ทธ๋•Œ (ํ•˜๊ณ  ์‹ถ์„ ๋•Œ) ์‚ฌ์šฉ

โœ”๏ธ ajax
browser์™€ web server๊ฐ€ page๋ฅผ reloadํ•˜์ง€ ์•Š๊ณ  JavaScript๋ฅผ ํ†ตํ•ด ์†Œํ†ตํ•˜๋Š” ๊ฒƒ
e.g. ๊ฒ€์ƒ‰์ฐฝ์— ๊ฒ€์ƒ‰์–ด๋กœ ์ดˆ์„ฑ, ์ค‘์„ฑ, ์ข…์„ฑ์„ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ํ†ต์‹ ํ•จ

promise

  • ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ๊ทธ ์ž‘์—…์˜ ์„ฑ๊ณต ์—ฌ๋ถ€์— ๋”ฐ๋ผ ํ‘œ์ค€ํ™”๋œ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ
  • .then(), .catch() ๋‘ ๊ฐ€์ง€ method๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ (๋‘˜ ๋‹ค callback ํ•จ์ˆ˜๋ฅผ ๋ฐ›์Œ)
    • promise ํ•จ์ˆ˜๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฉด .then() method๊ฐ€ response ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•จ
    • promise ํ•จ์ˆ˜๊ฐ€ ์‹คํŒจํ•˜๋ฉด .catch() method๊ฐ€ reason ๊ฐ์ฒด๋กœ ์‹คํŒจ ์‚ฌ์œ ๋ฅผ ์ „๋‹ฌํ•จ
  • web browser์™€ web server๊ฐ€ ํ†ต์‹ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” patch API๊ฐ€ promise๋ผ๋Š” data type์„ ์‚ฌ์šฉํ•จ (returnํ•จ)
    • ๋น„๋™๊ธฐ ์‹คํ–‰์ผ ํ™•๋ฅ ์ด ๋†’์Œ

nested promise

  • .then() ์•ˆ์— ๋˜ .then()...

promise chaining

  • promise๋ฅผ ํ†ตํ•ด .then()์„ ๋ฐ”๊นฅ์œผ๋กœ ์—ฐ๊ฒฐํ•œ ๊ฒƒ
fetch('https://jsonplaceholder.typicode.com/posts')
  .then(function(response) {
  return response.json();
  })
  .catch(function(reason) {
    console.log('reason', reason);
  })
  .then(function(data) {
    console.log('data', data);
  });

3. Async, Await

timer(1000, function() {
  console.log('์ž‘์—…');
  timer(1000, function() {
    console.log('์ž‘์—…');
    timer(1000, function() {
      console.log('์ž‘์—…');
    });
  });
});

// โ†“

timer(1000)
  .then(function() {
    console.log('์ž‘์—…')
    return timer(1000);
  })
  .then(function() {
    console.log('์ž‘์—…')
    return timer(1000);
  })
  .then(function() {
    console.log('์ž‘์—…')
  })

// โ†“

async function run() {
  await timer(1000)
    console.log('์ž‘์—…')
  await timer(1000)
    console.log('์ž‘์—…')
  await timer(1000)
    console.log('์ž‘์—…')
}
run();

async๋Š” ํ•จ์ˆ˜๋ฅผ promise๋กœ returnํ•ด ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค.


new Promise

๋‚ด๊ฐ€ promise ๋งŒ๋“ค๊ธฐ (์ด์ „๊นŒ์ง€๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๋งŒ๋“  promise ์“ฐ๊ธฐ)

function job1() {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            reject("job1 fail!");
        }, 2000);
    });
}
function job2() {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            resolve("job2 ok!");
        }, 2000);
    });
}

job1()
    .then(function(data) {
        console.log("data1", data);
        return job2();
    })
    .catch(function(reason) {
        console.log("reason", reason);
        // return Promise.reject(reason); ๋‹ค์Œ์— ์˜ค๋Š” then (job2) ์„ ์‹คํ–‰์‹œํ‚ค๊ณ  ์‹ถ์ง€ ์•Š์„ ๋•Œ
    })
    .then(function(data) {
        console.log("data2", data);
    })

all, race

  • Promise.all([])
    • ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋๋‚˜๋Š” code ๋‹ค์Œ์— callback ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ฒŒ ํ•จ
function timer(time) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            resolve(time);
        }, time);
    });
}
console.time("Promise.all");
Promise.all([timer(1000), timer(2000), timer(3000)]).then(function(result) {
    console.log("result", result); // result (3) [1000, 2000, 3000]
    console.timeEnd("Promise.all"); // Promise.all: 3001.748046875 ms
});
  • Promise.race([])
    • ๊ฐ€์žฅ ๋จผ์ € ๋๋‚˜๋Š” code ๋‹ค์Œ์— callback ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ฒŒ ํ•จ
function timer(time) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            resolve(time);
        }, time);
    });
}
console.time("Promise.race");
Promise.race([timer(1000), timer(2000),timer(3000)]).then(function(result) {
    console.log("result", result); // result 1000
    console.timeEnd("Promise.race"); // Promise.race: 1002.871826171875 ms
});



Endnote

MDN - Callback Function
{json} placeholder

๐Ÿ™‡ the source of this content

์ƒํ™œ์ฝ”๋”ฉ
์ง€์‹์ง€๋„ ์„œ๋ง

profile
๋Š๋ ค๋„ ํ•  ๊ฑฐ์•ผ.......

0๊ฐœ์˜ ๋Œ“๊ธ€