'Async'에 해당하는 글 1건

비동기가 항상 발목을 잡아버리네요 ... 


이전에 포스팅을 했던 콜백헬  http://moonna.tistory.com/14  의 연장선이라고 보시면 편합니다


이전에는 단순히 async.waterfall 을 이용해 순차적으로 실행을해줬습니다 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
        async.waterfall([
                                            function(scheduleId,callback){
                                                 console.log("next_result 3")
                                                 console.log(scheduleId)
                                                 functions.ScheduleFeatch(scheduleId);
                                                setTimeout(function() {
                                                callback(null,config.schedule.since_id)
                                            }, 3000);
                                            }
                                            ,
                                            function(scheduleId,callback){
                                                 console.log("next_result 4")
                                                 console.log(scheduleId)
                                                 functions.ScheduleFeatch(scheduleId);
                                                setTimeout(function() {
                                                callback(null,config.schedule.since_id)
                                            }, 3000);
                                            }
                                             ,
                                            function(scheduleId,callback){
                                                 console.log("next_result 5")
                                                 console.log(scheduleId)
                                                 //next_result 없을때 값처리해야됨
                                                 functions.ScheduleFeatch(scheduleId);
                                                setTimeout(function() {
                                                callback(null,config.schedule.since_id)
                                            }, 3000);
                                            }
                                           
                                            ]
                                        );
cs


실행순서는 


"next result3" > done > "next result4" > done > "next result5" > done


이런식으로 진행됩니다 


위의 async.waterfall 을 배열의 인덱스 값만큼 반복 시켜주려면 어찌해줘야 하나 고민하다 stackoverflow 에 해결책이 있었습니다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var async = require('async')
var users = ['a','b','c']
 
async.forEachOfLimit(users, 1function(user, index, cb) {
 
    console.log(index + ': ' + user)
 
    async.waterfall([
        function(callback) {
 
           // add something function
 
            callback(null);
        }
    ], function (err, result) {
        console.log(index + ": done")
        cb()
    });
 
}, function() {
    console.log('ALL done')
})
cs



배열의 값만 큼 순차적으로 비동기통신이 되었습니다 





WRITTEN BY
내가달이다

,