Async: Async๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๊ธฐ ์„ค์ •

์— ๋งŒ๋“  2016๋…„ 10์›” 25์ผ  ยท  4์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: caolan/async

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์™€ ๊ด€๋ จ๋œ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ์ค‘ ํ•˜๋‚˜์—์„œ ๋น„๋™๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์ „์— ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๊ธฐ๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค(๋น„๋™๊ธฐ ์ œ๊ณต ์ฝœ๋ฐฑ์— ๋Œ€ํ•œ ์ฐธ์กฐ ํฌํ•จ). ๊ทธ๋Ÿฌ๋‚˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ฝ”๋“œ๋Š” ์ฆ‰์‹œ ์ถ”๊ฐ€ ์ฝœ๋ฐฑ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ๋ฌธ์ œ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ž…๋‹ˆ๋‹ค.

async.auto({
  // This needs to run first, but it will finish second
  event: [(next) => {
    emitter.once('awesome', next);
  }],

  // This needs to finish near the end
  checkResults: ['event', (results, next) => {
    assert(results.event.status == 200);
    assert(results.event.msg == 'cool');

    // Do other async stuff...
    somethingAsync(next);
  }],

  // This has to start second, but before the first one finishes
  emit: ['event', (results, next) => {
    event.emit('awesome', {msg: 'cool'}, next);
  }],

  checkEmit: ['emit', (results, next) => {
    // some sort of check that can be async
  },
], done);

event ๋Š” ๋จผ์ € ์‹œ์ž‘ํ•ด์•ผ ํ•˜์ง€๋งŒ emit ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ๊นŒ์ง€ ์™„๋ฃŒ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. emit ๋Š” event ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜์ง€๋งŒ ์™„๋ฃŒ๋˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค(์ผ๋ช… ๊ทธ๋ƒฅ ํ•ธ๋“ค๋Ÿฌ ์„ค์ •). ๋”ฐ๋ผ์„œ ์ด๊ฒƒ์€ ๋๋‚˜์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ˜„์žฌ ๋น„๋™๊ธฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๊นŒ? (๊นจ๋—ํ•˜๊ฒŒ)

์ด๊ฒƒ์ด ๋น„๋™๊ธฐ์‹์œผ๋กœ ๊ตฌํ˜„๋  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์†”๋ฃจ์…˜์ž…๋‹ˆ๋‹ค.

async.auto({
  listen: [(next, done) => {
    client.on(done);
    return next();
  },

  ...
}, callback);

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

question

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋‚˜๋Š” ๊ทธ๊ฒƒ์ด ๋‚ด๊ฐ€ ๋ณธ ๊ฒƒ ์ค‘ ๊ฐ€์žฅ ๋ฏธ์นœ ์ž๋™ ์‚ฌ์šฉ ์‚ฌ๋ก€๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค :building_construction:

๋ชจ๋“  4 ๋Œ“๊ธ€

์•ˆ๋…•ํ•˜์„ธ์š” @Saevon , ์งˆ๋ฌธ ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

๋น ๋ฅด๊ณ  ๊นจ๋—ํ•œ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

async.auto({
    // This needs to finish near the end
  checkResults: [(next) => {
    return next(null, (results, next) => {
      assert(results.event.status == 200);
      assert(results.event.msg == 'cool');

      // Do other async stuff...
      // somethingAsync(next);
    });
  }],

  // This needs to run first, but it will finish second
  event: ['checkResults', (handler, next) => {
    emitter.once('awesome', handler.checkResults);
    return next();
  }],

  // This has to start second, but before the first one finishes
  emit: ['event', (results, next) => {
    // Should this be emitter.emit instead of event.emit?
    event.emit('awesome', {msg: 'cool'}, next);
  }],

  checkEmit: ['emit', (results, next) => {
    // the results of `checkResults` will be in `results.emit`
    // as the handler on 'awesome' was passed the `next`
    // callback from `emit`

    // some sort of check that can be async
    yourChecks(next);
  }]
}, function done(err) {
    // everything finished running, unless `err` !== `null`
});

๋ณธ์งˆ์ ์œผ๋กœ event ๊ฐ€ checkResults ์˜ ํ•ธ๋“ค๋Ÿฌ์— ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ์— event ์™€ checkResults ์˜์กด์„ฑ์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ๋ฟ์ž…๋‹ˆ๋‹ค. checkResults ๋Š” ์ด์ œ event ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

์‹คํ–‰ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
checkResults --> event --> emit --> handler (passed to event from checkResults) --> checkEmit --> done .

๋ถˆ๋ถ„๋ช…ํ•œ ๊ฒƒ์ด ์žˆ์œผ๋ฉด ์•Œ๋ ค์ฃผ์‹ญ์‹œ์˜ค.

๋„ค, ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ๊ฐ์‚ฌ ํ•ด์š”!

๋‚˜๋Š” ๊ทธ๊ฒƒ์ด ๋‚ด๊ฐ€ ๋ณธ ๊ฒƒ ์ค‘ ๊ฐ€์žฅ ๋ฏธ์นœ ์ž๋™ ์‚ฌ์šฉ ์‚ฌ๋ก€๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค :building_construction:

๋™์˜. ์ผ๋ฐ˜์ ์œผ๋กœ ๋น„๋™๊ธฐ ์ฝœ๋ฐฑ๊ณผ ์ด๋ฒคํŠธ ์ด๋ฏธํ„ฐ๋Š” ์ž˜ ์„ž์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค...

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰