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
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๋ฅผ ๋ฐํํ๋ ๋ฐ ์คํจํ์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
@pawelotto ๋ ธ๋ promisify util์ ์ฌ์ฉํ ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. ํ ์คํธํ์ง๋ ์์์ง๋ง ๊ธฐ๋ณธ ์ฝ๋ฐฑ ํจ์ ์๋ช ์ด ํธํ๋๊ธฐ ๋๋ฌธ์ ์๋ํ ๊ฒ์ด๋ผ๊ณ ๊ฐ์ ํฉ๋๋ค.
๋ฐ๋ผ์ ์์ ์๋ (์ค๋ฅ ์ฒ๋ฆฌ ์์ด) ๋ค์๊ณผ ๊ฐ์ด ๋ฉ๋๋ค.