Async: async/await๋กœ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•

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

var async = require('async');

var workPool = async.queue(async (task, taskComplete)=>{
  console.log(1, taskComplete);

  async.retry(3, async (retryComplete)=>{

    console.log(2, retryComplete);
    // retryComplete(null, task);

  });
});

workPool.push("test");

์™œ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๋ฅผ ์–ป์—ˆ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

$ node test.js
1 undefined
2 undefined
question

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

@pawelotto ๋…ธ๋“œ promisify util์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธํ•˜์ง€๋Š” ์•Š์•˜์ง€๋งŒ ๊ธฐ๋ณธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์„œ๋ช…์ด ํ˜ธํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ž‘๋™ํ•  ๊ฒƒ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์œ„์˜ ์˜ˆ๋Š” (์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ์—†์ด) ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฉ๋‹ˆ๋‹ค.

import async from "async"
import {promisify} from "util"

const promiseMapLimit = promisify(async.mapLimit)
const results = await promiseMapLimit(files, async file => { // <- no callback!
    const text = await util.promisify(fs.readFile)(dir + file, 'utf8')
    const body = JSON.parse(text) // <- a parse error herre will be caught automatically
    if (!(await checkValidity(body))) {
        throw new Error(`${file} has invalid contents`) // <- this error will also be caught
    }
    return body // <- return a value!
})

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

https://caolan.github.io/async/

Async๋Š” ๋…ธ๋“œ ์Šคํƒ€์ผ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ—ˆ์šฉํ•˜๋Š” ๋ชจ๋“  ๊ณณ์—์„œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๋Š” ๊ทธ๋“ค์—๊ฒŒ ์ฝœ๋ฐฑ์„ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ณ  ๋Œ€์‹  ๋ฐ˜ํ™˜ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๊ณ  ๋˜์ ธ์ง„ ์•ฝ์† ๊ฑฐ๋ถ€ ๋˜๋Š” ์˜ค๋ฅ˜๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

async.mapLimit(files, async file => { // <- no callback!
    const text = await util.promisify(fs.readFile)(dir + file, 'utf8')
    const body = JSON.parse(text) // <- a parse error herre will be caught automatically
    if (!(await checkValidity(body))) {
        throw new Error(`${file} has invalid contents`) // <- this error will also be caught
    }
    return body // <- return a value!
}, (err, contents) => {
    if (err) throw err
    console.log(contents)
})

์˜ˆ, Async๋Š” async ํ•จ์ˆ˜์— ์ฝœ๋ฐฑ์„ ์ „๋‹ฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ตœ์ข… ์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–ป์Šต๋‹ˆ๊นŒ(์ด ์˜ˆ์ œ์—์„œ์™€ ๊ฐ™์ด ์ฝ˜์†”์— ๊ธฐ๋กํ•˜๋Š” ๋Œ€์‹ )?

async ํ•จ์ˆ˜์—์„œ ๊ฐ’์„ return ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

์ •ํ™•ํžˆ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” async ํ•จ์ˆ˜๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ๋‚ด๊ฐ€ ํ™•์ธํ•œ ๊ฒƒ์—์„œ ๊ทธ๋“ค์€ ๋ฌดํšจ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด
ํ•  ๋•Œ๊นŒ์ง€

@pawelotto ๋…ธ๋“œ promisify util์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธํ•˜์ง€๋Š” ์•Š์•˜์ง€๋งŒ ๊ธฐ๋ณธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์„œ๋ช…์ด ํ˜ธํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ž‘๋™ํ•  ๊ฒƒ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์œ„์˜ ์˜ˆ๋Š” (์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ์—†์ด) ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฉ๋‹ˆ๋‹ค.

import async from "async"
import {promisify} from "util"

const promiseMapLimit = promisify(async.mapLimit)
const results = await promiseMapLimit(files, async file => { // <- no callback!
    const text = await util.promisify(fs.readFile)(dir + file, 'utf8')
    const body = JSON.parse(text) // <- a parse error herre will be caught automatically
    if (!(await checkValidity(body))) {
        throw new Error(`${file} has invalid contents`) // <- this error will also be caught
    }
    return body // <- return a value!
})

์–ด์ฉŒ๋ฉด ๋‹น์‹ ์€ neo-async๋ฅผ ์‹œ๋„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

@kazaff ๋ฉ‹์ ธ์š” ๋ชฐ๋ž์–ด์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

@kazaff ๋ ˆ์ „๋“œ!

@kazaff b4dnewz์˜ ๋‹ต๋ณ€์—์„œ neo-async๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ reulst๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.

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