μ΄λ€ λ²μ μ λΉλκΈ°λ₯Ό μ¬μ©νκ³ μμ΅λκΉ?
3.1.0
λ¬Έμ κ° λ°μν νκ²½(λ
Έλ λ²μ /λΈλΌμ°μ λ²μ )
λ
Έλ 12.9.1, npm 6.10.2, λΈλΌμ°μ ν΄λΉ μμ
λ νμ΄?
λ¬Έμ μ stackoverflowμ μ€λ λκ° μμ΅λλ€.
https://stackoverflow.com/questions/57622495/async-maplimit-with-promise/57659221#57659221
κΈ°λ³Έμ μΌλ‘ λ€μ μ½λκ° μμ΅λλ€.
async = require('async');
let numPromise = async.mapLimit(['1','2','3','4','5'], 3, function(num, callback){
setTimeout(function(){
num = num * 2,
console.log(num);
callback(null, num);
},
2000);
})
numPromise
.then((result) => console.log("success:" + result))
.catch(() => console.log("no success"));
λ¬΄μ¨ μΌμ΄ μΌμ΄λ κ²μ΄λΌκ³ μμνμ΅λκΉ?
μ€λ₯ μμ΄ μ€ννλ €λ©΄ 'numPromise'μ μ½μμ΄ ν¬ν¨λμ΄μΌ ν©λλ€. μ½μμ '2,4,6,8,10' λ° ' μ±κ³΅: 2,4,6,8,10'μ κΈ°λ‘ν΄μΌ ν©λλ€
μ€μ κ²°κ³Όλ μ΄λ λμ?
μ€λ₯κ° λ°μν©λλ€. TypeError: μ μλμ§ μμ 'then' μμ±μ μ½μ μ μμ΅λλ€.
μ°Έκ³ : 'async' λμ 'promise-async' λͺ¨λμ μ¬μ©νλ©΄ μ΄ μ½λκ° μ μλν©λλ€. λ¬Έμμ λ°λ₯΄λ©΄ μ½λ°±μ΄ μ 곡λμ§ μμΌλ©΄ async.mapLimit(λ° κΈ°ν)κ° μ½μμ λ°ννμ§λ§ μ μλμ§ μμ΅λλ€. μμ§ μλνλ μνμ μ°Ύμ μ μμ΅λλ€('μν νμ' λ¬Έμ μ λν μ μ μλ μ°Έμ‘°νμΈμ).
const async = require('async');
const delay = require('util').promisify(setTimeout);
const numPromise = async.mapLimit(['1','2','3','4','5'], 3, async num => delay(200).then(() => num*2))
// or const numPromise = async.mapLimit(['1','2','3','4','5'], 3, async num => {
// await delay(200);
// return num*2;
// })
numPromise.then(console.log)
λ§μ΄, ν©λ¦¬μ μΈ μμ
λλ€. λΆννλ "SyntaxError: awaitλ λΉλκΈ° ν¨μμμλ§ μ ν¨ν©λλ€"('await async.mapLimit'μ κ²½μ°)λ₯Ό μ 곡ν©λλ€.
κ³ λ €ν΄μΌ ν λ€λ₯Έ μ¬νμ΄ μμ΅λκΉ?
μ€λ₯ λ©μμ§λ λͺ¨λ κ²μ λ§νλ©°, λκΈ°λ μ΅μμ μμ€ μ μμ΄ κ΅¬νλ λκΉμ§ λΉλκΈ° ν¨μμμλ§ μ ν¨ν©λλ€.
μ, μμ ν μλνλ μμ λ₯Ό μ°Ύκ³ μμμ΅λλ€. μ΄κ²μ΄ μ κ° μ€μ λ‘ κ³ μ¬νλ λΆλΆμ΄κΈ° λλ¬Έμ λλ€. κ·Έλλ μ κ²½μ°μ§ λ§μΈμ. λ§μΉ¨λ΄ μμνκ³ μ€ννμ΅λλ€.
const myAsyncFunction = async function(){
//const numPromise = await async.mapLimit(['1','2','3','4','5'], 3, async num => delay(200).then(() => num*2))
const numPromise = await async.mapLimit(['1','2','3','4','5'], 3, async num => {
await delay(2000);
console.log(num*2);
return num*2;
})
//numPromise.then(console.log)
return numPromise;
}
myAsyncFunction()
.then((result) => console.log(result));
μ΄μ μλμ 무μμ΄ λ€λ₯Έμ§ μμ§ νμ€νμ§ μμ§λ§ μ΄μ λν κ²°λ‘ μ νμΈνκ³ μΆκ°ν κ²μ
λλ€.
μ΄μ¨λ λ΄ μμ μ μ΄κ²μ΄ μμ ν μ§κ΄μ μ΄μ§ μλ€λ κ²μ
λλ€. λ°λΌμ μ΄μ κ°μ μκ° λμμ΄ λ μ μμ΅λλ€!
κ·Έλ¦¬κ³ ... λλ μ¬μ ν async.mapLimitμ΄ κ·Έ μμ²΄λ‘ νλ²ν Promiseλ₯Ό λ°νν΄μΌ ν λ μ μ°λ¦¬κ° λͺ¨λ async/awaitλ₯Ό κ°μΈμΌ νλμ§ μμ ν μ΄ν΄νμ§ λͺ»ν©λλ€...
μ£μ‘ν©λλ€. μ΄μ μ½λμμ λκΈ°νλ κ²μ μμμ΅λλ€. μμ νμ΅λλ€.
μ, μ΄μ μμ ν νμΈνμ΅λλ€ :-) κ·Έλμ μλ§λ μ£Όμ λ¬Έμ λ iterate ν¨μκ° μμ ν λΉλκΈ°μ μ΄μ§ μλ€λ κ²μ΄μμκΉμ?
μ΄μ¨λ μ΄κ²μ λ§μΉ 맀λ ₯μ²λΌ μλνκ³ μλ§λ μ’μ μμΌ κ²μ
λλ€! μ μ ν¨κ» ν΄μ£Όμ
μ κ°μ¬ν©λλ€!!
μ΄μ λ https://github.com/caolan/async/issues/1673 μ μ°Έμ‘°
async.mapLimit(['1','2','3','4','5'], 3, async num => delay(200).then(() => num*2)) // works
async.mapLimit(['1','2','3','4','5'], 3, num => delay(200).then(() => num*2)) // doesn't work
Promise.all(['1','2','3','4','5'].map(num => delay(200).then(() => num*2))) // works (plain promises)
λΉμ μ λμ μμ ν κ°μ λ¬Έμ λ₯Ό κ²ͺμμ΅λλ€.
λ΄ μ£Όμ μμ μ λ€μκ³Ό κ°μ΅λλ€. λ¬Έμμλ "Returns: Promise if no callback ishanded"μ΄ λμ μμ΅λλ€. λ°λΌμ μ½λ°± λ³νμ΄ μλ κ²½μ° μ΄λ₯Ό κ·Έλ₯ λλ©΄ μ΄λ»κ² νμνλ€κ³ μκ°νκ² λ κΉμ? μ½λ°± λ²μ μμ ν€μλ μμ΄ μλν λ 'async' ν€μλλ₯Ό μΆκ°ν©λλ€. λν 'promise-async' λͺ¨λμ μ¬μ©νλ©΄ μμλλ‘ μ ννκ² μλν©λλ€.
λν μμ 'Promise.all' μμ κ° μλνλ μ΄μ λ₯Ό μμ§ μ΄ν΄νμ§ λͺ»νμ΅λλ€. μ΄κ²μ μ λ§ νΌλμ€λ½μ΅λλ€.
iteratee ν¨μλ async
μ΄κ±°λ μ½λ°±μ μ¬μ©νλ κ²½μ° μ΅μ’
μ½λ°±μ΄ μλ΅λ κ²½μ° μ½μ λ°νμ μν₯μ λ―ΈμΉμ§ μμ΅λλ€. μ΄κ²μ λ²κ·Έμ
λλ€.
λλ μ΄κ²μ΄ #1685μ κ΄λ ¨μ΄ μλ€κ³ μ₯λ΄ν κ²μ΄λ€.
λλ μ΄κ²μ΄ #1685μ κ΄λ ¨μ΄ μλ€κ³ μ₯λ΄ν κ²μ΄λ€.
λ΄ μ½λ μνμ μΌλΆ λ²μ μμλ μ΄μ μ ν΄λΉ λ¬Έμ κ° μμλ κ²μΌλ‘ κΈ°μ΅ν©λλ€. λλ λΆλͺ ν κ·Έκ²μ΄ μλͺ»λ μ½λ©μΌλ‘ μΈν΄ λ°μνλ€κ³ κ°μ νμ΅λλ€. κ·Έλμ λ€λ₯Έ λ§μΌλ‘λ λνλλ€λ μ¬μ€μ΄ ν₯λ―Έλ‘μ΅λλ€.
μ΄μ λν΄ λ μ΄ν΄λ³΄μλ€.
mapLimit
μ΄(κ°) μλν λλ‘ μλν©λλ€. μ¬κΈ°μ μ¬λλ€μ΄ 보λ λͺ¨λ λ¬Έμ λ μ½μμ λ°ννλ ν¨μ λλ async
ν¨μλ₯Ό μ μ§νμ§ μλ μ»΄νμΌλ¬(μ: babel, typescript)λ₯Ό κ°μ§νλ λ° μ νμ΄ μκΈ° λλ¬Έμ
λλ€.
μ΄μ λν΄ λ μ΄ν΄λ³΄μλ€.
mapLimit
μ΄(κ°) μλν λλ‘ μλν©λλ€. μ¬κΈ°μ μ¬λλ€μ΄ 보λ λͺ¨λ λ¬Έμ λ μ½μμ λ°ννλ ν¨μ λλasync
ν¨μλ₯Ό μ μ§νμ§ μλ μ»΄νμΌλ¬(μ: babel, typescript)λ₯Ό κ°μ§νλ λ° μ νμ΄ μκΈ° λλ¬Έμ λλ€.
async
κ° μ»΄νμΌμ 보쑴λμ§ μλ μλ리μ€λ₯Ό μ²λ¦¬νλ κ°μ₯ μ’μ λ°©λ²μ 무μμ
λκΉ?
async
ν¨μλ₯Ό asyncify
λνν©λλ€. http://caolan.github.io/async/v3/global.html#AsyncFunction
κ°μ₯ μ μ©ν λκΈ