<p>async.mapLimit은 약속을 λ°˜ν™˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.</p>

에 λ§Œλ“  2019λ…„ 08μ›” 27일  Β·  14μ½”λ©˜νŠΈ  Β·  좜처: caolan/async

μ–΄λ–€ λ²„μ „μ˜ 비동기λ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆκΉŒ?
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)

λͺ¨λ“  14 λŒ“κΈ€

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

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰