Async: es6 비동기 ν•¨μˆ˜λ₯Ό λŒ€κΈ°μ—΄μ— λ„£λŠ” 방법

에 λ§Œλ“  2017λ…„ 05μ›” 12일  Β·  4μ½”λ©˜νŠΈ  Β·  좜처: caolan/async

κ·Έλž˜μ„œ λͺ‡ 가지 async/await κΈ°λŠ₯을 가지고 있으며 λŒ€κΈ°μ—΄μ— λ„£κ³  μ‹ΆμŠ΅λ‹ˆλ‹€. νŠΉμ • websocket μ΄λ²€νŠΈμ—μ„œ "λ¬΄μž‘μœ„λ‘œ" μ‹€ν–‰λ˜μ§€λ§Œ μ°¨λ‘€λ‘œ μ‹€ν–‰ν•΄μ•Ό ν•©λ‹ˆλ‹€. κ·Έλž˜μ„œ 이 μ½”λ“œλ‘œ 이것을 ν…ŒμŠ€νŠΈν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

const aida = {
  async launch() {
    await someFunction();
  },
  async login() {
    await Auth();
    await UserSettings();
  },
  async goToMonth() {
    await JumpToDefaultMonth();
  }
};


const q = async.queue(function(task, callback) {
  console.log('hello ' + task.name);
  callback();
}, 1);

q.drain = function() {
  console.log('all items have been processed');
};

q.push(aida.launch());
q.push(aida.login());
q.push(aida.goToMonth());

예λ₯Ό λ“€μ–΄ login() λŠ” launch() κ°€ μ™„λ£Œλœ ν›„μ—λ§Œ μ‹€ν–‰λ©λ‹ˆλ‹€.
λ‚΄κ°€ λ­”κ°€ 잘λͺ»ν•˜κ³  μžˆμŠ΅λ‹ˆκΉŒ, μ•„λ‹ˆλ©΄ 이 λΌμ΄λΈŒλŸ¬λ¦¬κ°€ λ‚˜μ—κ²Œ λ§žμ§€ μ•ŠμŠ΅λ‹ˆκΉŒ?

question queue

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

@codeofsumit μ£„μ†‘ν•©λ‹ˆλ‹€. 이전 λŒ“κΈ€μ΄ μ•½κ°„ 잘λͺ»λ˜μ—ˆλ‹€λŠ” 것을 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€. queue κ°€ λ‹€λ₯Έ κΈ°λŠ₯κ³Ό ν˜Όλ™λ˜μ—ˆμŠ΅λ‹ˆλ‹€. λ‚΄κ°€ λͺ…ν™•νžˆ ν•  수 있기λ₯Ό λ°”λžλ‹ˆλ‹€.

  1. λŒ€κΈ°μ—΄μ€ μ–΄λ–»κ²Œ μ‹œμž‘λ©λ‹ˆκΉŒ? 첫 번째 ν‘Έμ‹œ ν›„ μ‹œμž‘λ˜μ§€ μ•Šμ€ 것 κ°™μŠ΅λ‹ˆλ‹€.

queue λŠ” task , 즉 q.push λ₯Ό ν˜ΈμΆœν•  λ•Œ μ‹œμž‘ν•΄μ•Ό ν•©λ‹ˆλ‹€. μ½”λ“œ μƒ˜ν”Œμ„ μ œκ³΅ν•  수 μžˆμŠ΅λ‹ˆκΉŒ? μœ„μ˜ μ½”λ“œλŠ” λ‚˜λ₯Ό μœ„ν•΄ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

μ°Έκ³  사항: async 와 ν•¨κ»˜ es7 async ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜κ³  μ½”λ“œλ₯Ό νŠΈλžœμŠ€νŒŒμΌν•˜λŠ” 경우 async.asyncify 둜 λž˜ν•‘ν•΄μ•Ό ν•©λ‹ˆλ‹€. μžμ„Έν•œ λ‚΄μš©μ€ 이 λŒ“κΈ€ 을 μ°Έμ‘°ν•˜μ„Έμš”.

  1. ν•¨μˆ˜μ— λ§€κ°œλ³€μˆ˜λ₯Ό μ „λ‹¬ν•˜λ €λ©΄ μ–΄λ–»κ²Œ ν•΄μ•Ό ν•©λ‹ˆκΉŒ?

μ—¬κΈ°μ„œ λ‚΄κ°€ ν˜Όλž€μŠ€λŸ¬μ›Œμ‘Œλ‹€. worker 생성 μ‹œ async.queue μ „λ‹¬ν•˜λŠ” launch , login 및 goToMonth λ₯Ό μ‹€ν–‰ν•΄μ•Ό ν•˜λŠ” 경우 worker ν•¨μˆ˜λ₯Ό λ„£μœΌλ©΄ λ©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄,

const q = async.queue(function(task, callback) {
  async.series([
    async function() {
      await aida.launch(task.foo, task.boo);
    },
    async function() {
      await aida.login(task.baz);
    },
    // etc...
  ], callback);
}, 1);

// on a websocket event
q.push({foo: websocketData.foo, bar: websocketData.bar, baz: websocketData.baz});

그렇지 μ•ŠμœΌλ©΄ ν•¨μˆ˜λ₯Ό λŒ€κΈ°μ—΄μ— λ„£μ–΄μ•Ό ν•˜λŠ” 경우 μ•½κ°„ ν•΄ν‚Ήλ˜μ§€λ§Œ λ‹€μŒκ³Ό 같이 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

const q = async.queue(async function(task) {
  await task.func.apply(null, task.args);
}, 1);

// on a websocket event
q.push({func: aida.launch, args: [websocketData.foo, websocketData.bar]});
q.push({func: aida.login, args: [websocketData.baz]});
q.push({func: aida.goToMonth, args: []});

λ°”λΌκ±΄λŒ€ 이것은 쑰금 더 λͺ…ν™•ν•©λ‹ˆλ‹€.

λͺ¨λ“  4 λŒ“κΈ€

μ•ˆλ…•ν•˜μ„Έμš” @codeofsumit , 질문 μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€!

μ½”λ“œλ₯Ό 보면 λ§ˆμ§€λ§‰ μ„Έ 쀄에 λ¬Έμ œκ°€ μžˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜κ³  queue ν•¨μˆ˜λ₯Ό ν‘Έμ‹œν•˜λŠ” 것과 λ°˜λŒ€λ‘œ λ°˜ν™˜ 값을 queue 에 ν‘Έμ‹œν•©λ‹ˆλ‹€. μ°Έκ³  ν•­λͺ© QueueObject λ¬Έμ„œλ₯Ό λŒ€ν•œ μžμ„Έν•œ 정보λ₯Ό μ›ν•˜μ‹œλ©΄ q.push . λ‹€μŒκ³Ό 같이 λ³€κ²½ν•΄ λ³΄μ‹­μ‹œμ˜€.

q.push(aida.launch);
q.push(aida.login);
q.push(aida.goToMonth);

당신이 가지고 있기 λ•Œλ¬Έμ— concurrency 의 1 , νμ—λ§Œ μ²˜λ¦¬ν•©λ‹ˆλ‹€ login ν›„ launch μ™„λ£Œν•©λ‹ˆλ‹€.

@hargasinski λ‚˜λŠ” κ·Έλ ‡κ²Œ μƒκ°ν•˜κ³  μ‹œλ„ν–ˆμ§€λ§Œ 두 가지 μƒˆλ‘œμš΄ 질문이

  1. λŒ€κΈ°μ—΄μ€ μ–΄λ–»κ²Œ μ‹œμž‘λ©λ‹ˆκΉŒ? 첫 번째 ν‘Έμ‹œ ν›„ μ‹œμž‘λ˜μ§€ μ•Šμ€ 것 κ°™μŠ΅λ‹ˆλ‹€.
  2. ν•¨μˆ˜μ— λ§€κ°œλ³€μˆ˜λ₯Ό μ „λ‹¬ν•˜λ €λ©΄ μ–΄λ–»κ²Œ ν•΄μ•Ό ν•©λ‹ˆκΉŒ?

@codeofsumit μ£„μ†‘ν•©λ‹ˆλ‹€. 이전 λŒ“κΈ€μ΄ μ•½κ°„ 잘λͺ»λ˜μ—ˆλ‹€λŠ” 것을 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€. queue κ°€ λ‹€λ₯Έ κΈ°λŠ₯κ³Ό ν˜Όλ™λ˜μ—ˆμŠ΅λ‹ˆλ‹€. λ‚΄κ°€ λͺ…ν™•νžˆ ν•  수 있기λ₯Ό λ°”λžλ‹ˆλ‹€.

  1. λŒ€κΈ°μ—΄μ€ μ–΄λ–»κ²Œ μ‹œμž‘λ©λ‹ˆκΉŒ? 첫 번째 ν‘Έμ‹œ ν›„ μ‹œμž‘λ˜μ§€ μ•Šμ€ 것 κ°™μŠ΅λ‹ˆλ‹€.

queue λŠ” task , 즉 q.push λ₯Ό ν˜ΈμΆœν•  λ•Œ μ‹œμž‘ν•΄μ•Ό ν•©λ‹ˆλ‹€. μ½”λ“œ μƒ˜ν”Œμ„ μ œκ³΅ν•  수 μžˆμŠ΅λ‹ˆκΉŒ? μœ„μ˜ μ½”λ“œλŠ” λ‚˜λ₯Ό μœ„ν•΄ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

μ°Έκ³  사항: async 와 ν•¨κ»˜ es7 async ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜κ³  μ½”λ“œλ₯Ό νŠΈλžœμŠ€νŒŒμΌν•˜λŠ” 경우 async.asyncify 둜 λž˜ν•‘ν•΄μ•Ό ν•©λ‹ˆλ‹€. μžμ„Έν•œ λ‚΄μš©μ€ 이 λŒ“κΈ€ 을 μ°Έμ‘°ν•˜μ„Έμš”.

  1. ν•¨μˆ˜μ— λ§€κ°œλ³€μˆ˜λ₯Ό μ „λ‹¬ν•˜λ €λ©΄ μ–΄λ–»κ²Œ ν•΄μ•Ό ν•©λ‹ˆκΉŒ?

μ—¬κΈ°μ„œ λ‚΄κ°€ ν˜Όλž€μŠ€λŸ¬μ›Œμ‘Œλ‹€. worker 생성 μ‹œ async.queue μ „λ‹¬ν•˜λŠ” launch , login 및 goToMonth λ₯Ό μ‹€ν–‰ν•΄μ•Ό ν•˜λŠ” 경우 worker ν•¨μˆ˜λ₯Ό λ„£μœΌλ©΄ λ©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄,

const q = async.queue(function(task, callback) {
  async.series([
    async function() {
      await aida.launch(task.foo, task.boo);
    },
    async function() {
      await aida.login(task.baz);
    },
    // etc...
  ], callback);
}, 1);

// on a websocket event
q.push({foo: websocketData.foo, bar: websocketData.bar, baz: websocketData.baz});

그렇지 μ•ŠμœΌλ©΄ ν•¨μˆ˜λ₯Ό λŒ€κΈ°μ—΄μ— λ„£μ–΄μ•Ό ν•˜λŠ” 경우 μ•½κ°„ ν•΄ν‚Ήλ˜μ§€λ§Œ λ‹€μŒκ³Ό 같이 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

const q = async.queue(async function(task) {
  await task.func.apply(null, task.args);
}, 1);

// on a websocket event
q.push({func: aida.launch, args: [websocketData.foo, websocketData.bar]});
q.push({func: aida.login, args: [websocketData.baz]});
q.push({func: aida.goToMonth, args: []});

λ°”λΌκ±΄λŒ€ 이것은 쑰금 더 λͺ…ν™•ν•©λ‹ˆλ‹€.

μ–΄, 이것은 맀우 μœ λ§ν•΄ λ³΄μž…λ‹ˆλ‹€. μ‹œκ°„μ„ λ‚΄μ–΄ μ΄λ ‡κ²Œ μžμ„Έν•˜κ²Œ μ„€λͺ…ν•΄μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€. λ§Žμ€ 도움이 λ©λ‹ˆλ‹€!

λ˜ν•œ: λ‚˜λŠ” 당신이 이 λ¦¬ν¬μ§€ν† λ¦¬μ˜ 문제λ₯Ό 맀우 λΉ λ₯΄κ²Œ ν•΄κ²°ν•˜κ³  μžˆλ‹€λŠ” 것을 μ•Œμ•„μ°¨λ ΈμŠ΅λ‹ˆλ‹€. πŸ‘ 맀우 μΈμƒμ μž…λ‹ˆλ‹€. κ³„μ†ν•΄μ„œ 쒋은 일을 ν•˜μ‹­μ‹œμ˜€!

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰