์ด๋ฒคํธ ํธ๋ค๋ฌ์ ๊ด๋ จ๋ ํตํฉ ํ ์คํธ ์ค ํ๋์์ ๋น๋๊ธฐ๋ฅผ ์ฌ์ฉํ๋ ๋ฐ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋ฌธ์ ๋ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๊ธฐ ์ ์ ์ด๋ฒคํธ ์ฒ๋ฆฌ๊ธฐ๋ฅผ ์ค์ ํด์ผ ํ๋ค๋ ๊ฒ์ ๋๋ค(๋น๋๊ธฐ ์ ๊ณต ์ฝ๋ฐฑ์ ๋ํ ์ฐธ์กฐ ํฌํจ). ๊ทธ๋ฌ๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ ์ฝ๋๋ ์ฆ์ ์ถ๊ฐ ์ฝ๋ฐฑ์ด ํ์ํฉ๋๋ค.
๋ค์์ ๋ฌธ์ ๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ฐ๋จํ ์์ ๋๋ค.
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);
๋๋ ์ด๋ฏธํฐ์ ๋ฆฌ์ค๋(ํ ์คํธ์ ์ฌ๋ฌ ๊ฐ ์์ ์ ์์)๋ฅผ ์์ ๋ฐฐ์ด๋ก ์คํํ์ฌ ๋ณ๋ ฌ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ค๊ณ ๋ ๋ฒ์งธ ๋ถ๋ถ์ผ๋ก ๊ฒ์ฌ๋ฅผ ์คํํฉ๋๋ค. ๊ทธ๋ฌ๋ ๊ธฐ์ ์ ์ผ๋ก ๋ณ๋ ฌ์ ์์ ์ ์์๋๋ก ์์ํ๋ ๋ฐ ํ์ํ์ง ์์ต๋๋ค(์๋ง ๊ทธ๋ด ๊ฐ๋ฅ์ฑ์ด ์์). ๋ํ ์ฝ๋๋ ํนํ ๋ ๋ณต์กํ ์ค์ ์์ ๋ ๋จ์ํด์ง๋๋ค.
์๋ ํ์ธ์ @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:
๋์. ์ผ๋ฐ์ ์ผ๋ก ๋น๋๊ธฐ ์ฝ๋ฐฑ๊ณผ ์ด๋ฒคํธ ์ด๋ฏธํฐ๋ ์ ์์ด์ง ์์ต๋๋ค...
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๋ ๊ทธ๊ฒ์ด ๋ด๊ฐ ๋ณธ ๊ฒ ์ค ๊ฐ์ฅ ๋ฏธ์น ์๋ ์ฌ์ฉ ์ฌ๋ก๋ผ๊ณ ์๊ฐํฉ๋๋ค :building_construction: