# Code coverage report for lib/async.js

## Statements: 94.88% (611 / 644) Branches: 86.91% (239 / 275) Functions: 95.41% (187 / 196) Lines: 94.85% (608 / 641) Ignored: none

All files » lib/ » async.js

    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925
    926
    927
    928
    929
    930
    931
    932
    933
    934
    935
    936
    937
    938
    939
    940
    941
    942
    943
    944
    945
    946
    947
    948
    949
    950
    951
    952
    953
    954
    955
    956
    957
    958
    959
    960
    961
    962
    963
    964
    965
    966
    967
    968
    969
    970
    971
    972
    973
    974
    975
    976
    977
    978
    979
    980
    981
    982
    983
    984
    985
    986
    987
    988
    989
    990
    991
    992
    993
    994
    995
    996
    997
    998
    999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178
    1179
    1180
    1181
    1182
    1183
    1184
    1185

    1

    1
    1

    1

    1

    1
    1

    1
    1

    1

    1
    200
    200
    200
    196
    196

    1
    168
    168
    176
    157
    157

    1

    1

    1
    138

    1
    39

    1
    328

    328
    480

    1
    104

    104
    148

    104

    1
    56
    54

    56

    1
    5
    11

    1

    1
    76
    76
    76
    76
    61
    61
    243
    243

    15
    15
    15
    53
    53

    1
    50259
    50259
    50259

    50259
    151
    151

    50259

    50259
    50299

    50259

    1
    19
    59

    1
    1
    1

    1

    1
    1
    1

    50236

    1

    5

    1

    5

    1

    9

    1

    45
    45
    45
    45
    45
    6

    39
    109

    1
    105
    16

    89
    89
    31

    1

    44
    44
    44
    1
    123
    123
    123
    29

    94
    90
    11

    79
    20

    59

    94

    44

    1

    9

    1

    32
    32
    32
    32
    32
    3

    29
    29
    29

    29
    119
    12

    107
    173
    173
    71
    71
    17

    71

    102
    102
    97
    97
    7
    7

    90

    1
    5
    19

    1
    8
    8

    1
    5
    12

    1
    25
    25
    25
    69
    67
    67

    25

    1
    1
    1
    8

    1
    8

    1

    11
    26
    25
    25

    10

    1

    1
    3

    1

    1
    3
    3
    9

    3
    9
    9
    6

    9

    3
    3

    6

    1

    1

    1
    3
    3
    9

    3
    9
    9
    3

    9

    3

    3

    1
    1

    1
    5
    15
    15
    7
    7

    8

    1
    1

    1

    3
    9
    9
    2
    2

    9

    3

    1

    3
    9
    9
    3
    3

    9

    3

    1
    2
    6
    6

    6

    2

    2
    6

    1
    6
    6

    1
    14
    14
    14
    14
    1

    13

    13
    1
    28

    1
    13
    19
    13
    13

    1
    25
    25
    47

    13
    25
    7

    13
    36
    1
    30
    30
    29

    30
    5
    5
    1

    5
    5

    25
    25

    36

    36
    36
    36
    23
    1

    22
    1

    1
    56
    54

    34
    19

    15

    1
    22
    13
    13

    1
    3
    3

    3
    2
    2
    2

    3

    1
    1
    13
    7
    7

    3
    13

    3
    3
    3

    3

    1
    8
    8
    1
    1

    7
    1

    1
    18
    19
    1

    18
    18
    18
    12

    6

    18

    6

    1
    13
    13

    13
    27
    27
    27
    23

    27
    27

    13

    1
    7

    1
    6

    1
    10
    10

    10
    21
    21
    21
    19

    21
    21

    10

    1
    1
    1
    25
    24

    25

    38
    46

    38

    9

    1
    6
    6
    6

    1
    3
    3
    9
    9
    9

    3

    1
    1

    1
    6
    5
    5

    5

    1

    1
    10
    10

    10
    10
    8

    2

    1
    6
    5
    5

    5

    1

    1
    10
    10

    10
    10
    8

    2

    1
    27
    3

    24
    1

    1
    92
    1

    91
    91
    84

    91

    2
    2

    89
    105

    105
    8

    97

    105
    28

    105

    26
    26

    84

    1
    1

    8

    207
    89

    89
    112

    89
    30

    89
    89
    89

    1
    89
    89
    89
    112

    89
    27

    89

    50

    5

    9

    3

    4
    3
    3

    3
    5

    26

    1
    21
    63

    20

    1

    1
    10

    1
    8

    8
    10
    10
    6

    4

    8

    1
    8

    8
    8
    8

    8

    8
    8

    8

    8
    2

    8

    2

    2
    8

    2

    2

    1
    6

    1
    2
    6
    6
    6
    6
    6
    2
    2

    4
    4
    8

    1
    1

    1
    7
    7
    7
    9

    1
    11
    11
    11
    11
    4
    4

    7
    1

    6
    6
    6
    6
    6
    6
    7

    7
    7
    7

    1
    2
    4

    1
    3
    3
    6

    3

    1
    2
    2
    10

    2

    1
    7
    7
    7
    7

    7
    7
    6

    1

    7
    16
    15
    15
    15

    6

    1
    3

    1
    1
    3
    3
    3
    3
    9

    3
    2
    2

    1

    1
    2
    2

    1
    1
    1

    1
    2
    2
    1
    50052
    2

    50050

    2

    1
    24
    50072
    50072
    50072
    50070
    50070
    50017
    50017

    53

    50072
    50072
    50072

    1

    1
    1

     /*!
     * async
     * https://github.com/caolan/async
     *
     * Copyright 2010-2014 Caolan McMahon
     * Released under the MIT license
     */
    (function () {

        var async = {};
        function noop() {}

        // global on the server, window in the browser
        var root, previous_async;

        Iif (typeof window == 'object' && this === window) {
            root = window;
        }
        else Eif (typeof global == 'object' && this === global) {
            root = global;
        }
        else {
            root = this;
        }

        Eif (root != null) {
          previous_async = root.async;
        }

        async.noConflict = function () {
            root.async = previous_async;
            return async;
        };

        function only_once(fn) {
            var called = false;
            return function() {
                if (called) throw new Error("Callback was already called.");
                called = true;
                fn.apply(this, arguments);
            };
        }

        function _once(fn) {
            var called = false;
            return function() {
                if (called) return;
                called = true;
                fn.apply(this, arguments);
            };
        }

        //// cross-browser compatiblity functions ////

        var _toString = Object.prototype.toString;

        var _isArray = Array.isArray || function (obj) {
            return _toString.call(obj) === '[object Array]';
        };

        function _isArrayLike(arr) {
            return _isArray(arr) || (
                // has a positive integer length property
                typeof arr.length === "number" &&
                arr.length >= 0 &&
                arr.length % 1 === 0
            );
        }

        function _each(coll, iterator) {
            return _isArrayLike(coll) ?
                _arrayEach(coll, iterator) :
                _forEachOf(coll, iterator);
        }

        function _arrayEach(arr, iterator) {
          var index = -1,
              length = arr.length;

          while (++index < length) {
            iterator(arr[index], index, arr);
          }
        }

        function _map(arr, iterator) {
          var index = -1,
              length = arr.length,
              result = Array(length);

          while (++index < length) {
            result[index] = iterator(arr[index], index, arr);
          }
          return result;
        }

        function _reduce(arr, iterator, memo) {
            _arrayEach(arr, function (x, i, a) {
                memo = iterator(memo, x, i, a);
            });
            return memo;
        }

        function _forEachOf(object, iterator) {
            _arrayEach(_keys(object), function (key) {
                iterator(object[key], key);
            });
        }

        var _keys = Object.keys || function (obj) {
            var keys = [];
            for (var k in obj) {
                if (obj.hasOwnProperty(k)) {
                    keys.push(k);
                }
            }
            return keys;
        };

        function _keyIterator(coll) {
            var i = -1;
            var len;
            var keys;
            if (_isArrayLike(coll)) {
                len = coll.length;
                return function next() {
                    i++;
                    return i < len ? i : null;
                };
            } else {
                keys = _keys(coll);
                len = keys.length;
                return function next() {
                    i++;
                    return i < len ? keys[i] : null;
                };
            }
        }

        function _baseSlice(arr, start) {
            start = start || 0;
            var index = -1;
            var length = arr.length;

            if (start) {
              length -= start;
              length = length < 0 ? 0 : length;
            }
            var result = Array(length);

            while (++index < length) {
              result[index] = arr[index + start];
            }
            return result;
        }

        function _withoutIndex(iterator) {
            return function (value, index, callback) {
                return iterator(value, callback);
            };
        }

        //// exported async module functions ////

        //// nextTick implementation with browser-compatible fallback ////

        // capture the global reference to guard against fakeTimer mocks
        var _setImmediate;
        Eif (typeof setImmediate === 'function') {
            _setImmediate = setImmediate;
        }

        Iif (typeof process === 'undefined' || !(process.nextTick)) {
            if (_setImmediate) {
                async.nextTick = function (fn) {
                    // not a direct alias for IE10 compatibility
                    _setImmediate(fn);
                };
                async.setImmediate = async.nextTick;
            }
            else {
                async.nextTick = function (fn) {
                    setTimeout(fn, 0);
                };
                async.setImmediate = async.nextTick;
            }
        }
        else {
            async.nextTick = process.nextTick;
            Eif (_setImmediate) {
                async.setImmediate = function (fn) {
                  // not a direct alias for IE10 compatibility
                  _setImmediate(fn);
                };
            }
            else {
                async.setImmediate = async.nextTick;
            }
        }

        async.forEach =
        async.each = function (arr, iterator, callback) {
            return async.eachOf(arr, _withoutIndex(iterator), callback);
        };

        async.forEachSeries =
        async.eachSeries = function (arr, iterator, callback) {
            return async.eachOfSeries(arr, _withoutIndex(iterator), callback);
        };

        async.forEachLimit =
        async.eachLimit = function (arr, limit, iterator, callback) {
            return _eachOfLimit(limit)(arr, _withoutIndex(iterator), callback);
        };

        async.forEachOf =
        async.eachOf = function (object, iterator, callback) {
            callback = _once(callback || noop);
            object = object || [];
            var size = object.length || _keys(object).length;
            var completed = 0;
            if (!size) {
                return callback(null);
            }
            _each(object, function (value, key) {
                iterator(object[key], key, only_once(done));
            });
            function done(err) {
              if (err) {
                  callback(err);
              }
              else {
                  completed += 1;
                  if (completed >= size) {
                      callback(null);
                  }
              }
            }
        };

        async.forEachOfSeries =
        async.eachOfSeries = function (obj, iterator, callback) {
            callback = _once(callback || noop);
            obj = obj || [];
            var nextKey = _keyIterator(obj);
            function iterate() {
                var sync = true;
                var key = nextKey();
                if (key === null) {
                    return callback(null);
                }
                iterator(obj[key], key, function (err) {
                    if (err) {
                        callback(err);
                    }
                    else {
                        if (sync) {
                            async.nextTick(iterate);
                        }
                        else {
                            iterate();
                        }
                    }
                });
                sync = false;
            }
            iterate();
        };

        async.forEachOfLimit =
        async.eachOfLimit = function (obj, limit, iterator, callback) {
            _eachOfLimit(limit)(obj, iterator, callback);
        };

        function _eachOfLimit(limit) {

            return function (obj, iterator, callback) {
                callback = _once(callback || noop);
                obj = obj || [];
                var nextKey = _keyIterator(obj);
                if (limit <= 0) {
                    return callback(null);
                }
                var done = false;
                var running = 0;
                var errored = false;

                (function replenish () {
                    if (done && running <= 0) {
                        return callback(null);
                    }

                    while (running < limit && !errored) {
                        var key = nextKey();
                        if (key === null) {
                            done = true;
                            if (running <= 0) {
                                callback(null);
                            }
                            return;
                        }
                        running += 1;
                        iterator(obj[key], key, function (err) {
                            running -= 1;
                            if (err) {
                                callback(err);
                                errored = true;
                            }
                            else {
                                replenish();
                            }
                        });
                    }
                })();
            };
        }

        function doParallel(fn) {
            return function (obj, iterator, callback) {
                return fn(async.eachOf, obj, iterator, callback);
            };
        }
        function doParallelLimit(limit, fn) {
            return function (obj, iterator, callback) {
                return fn(_eachOfLimit(limit), obj, iterator, callback);
            };
        }
        function doSeries(fn) {
            return function (obj, iterator, callback) {
                return fn(async.eachOfSeries, obj, iterator, callback);
            };
        }

        function _asyncMap(eachfn, arr, iterator, callback) {
            callback = _once(callback || noop);
            var results = [];
            eachfn(arr, function (value, index, callback) {
                iterator(value, function (err, v) {
                    results[index] = v;
                    callback(err);
                });
            }, function (err) {
                callback(err, results);
            });
        }

        async.map = doParallel(_asyncMap);
        async.mapSeries = doSeries(_asyncMap);
        async.mapLimit = function (arr, limit, iterator, callback) {
            return _mapLimit(limit)(arr, iterator, callback);
        };

        function _mapLimit(limit) {
            return doParallelLimit(limit, _asyncMap);
        }

        // reduce only has a series version, as doing reduce in parallel won't
        // work in many situations.
        async.inject =
        async.foldl =
        async.reduce = function (arr, memo, iterator, callback) {
            async.eachOfSeries(arr, function (x, i, callback) {
                iterator(memo, x, function (err, v) {
                    memo = v;
                    callback(err);
                });
            }, function (err) {
                callback(err || null, memo);
            });
        };

        async.foldr =
        async.reduceRight = function (arr, memo, iterator, callback) {
            var reversed = _map(arr, function (x) {
                return x;
            }).reverse();
            async.reduce(reversed, memo, iterator, callback);
        };

        function _filter(eachfn, arr, iterator, callback) {
            var results = [];
            arr = _map(arr, function (x, i) {
                return {index: i, value: x};
            });
            eachfn(arr, function (x, index, callback) {
                iterator(x.value, function (v) {
                    if (v) {
                        results.push(x);
                    }
                    callback();
                });
            }, function () {
                callback(_map(results.sort(function (a, b) {
                    return a.index - b.index;
                }), function (x) {
                    return x.value;
                }));
            });
        }

        async.select =
        async.filter = doParallel(_filter);

        async.selectSeries =
        async.filterSeries = doSeries(_filter);

        function _reject(eachfn, arr, iterator, callback) {
            var results = [];
            arr = _map(arr, function (x, i) {
                return {index: i, value: x};
            });
            eachfn(arr, function (x, index, callback) {
                iterator(x.value, function (v) {
                    if (!v) {
                        results.push(x);
                    }
                    callback();
                });
            }, function () {
                callback(_map(results.sort(function (a, b) {
                    return a.index - b.index;
                }), function (x) {
                    return x.value;
                }));
            });
        }
        async.reject = doParallel(_reject);
        async.rejectSeries = doSeries(_reject);

        function _detect(eachfn, arr, iterator, main_callback) {
            eachfn(arr, function (x, index, callback) {
                iterator(x, function (result) {
                    if (result) {
                        main_callback(x);
                        main_callback = noop;
                    }
                    else {
                        callback();
                    }
                });
            }, function () {
                main_callback();
            });
        }
        async.detect = doParallel(_detect);
        async.detectSeries = doSeries(_detect);

        async.any =
        async.some = function (arr, iterator, main_callback) {
            async.eachOf(arr, function (x, _, callback) {
                iterator(x, function (v) {
                    if (v) {
                        main_callback(true);
                        main_callback = noop;
                    }
                    callback();
                });
            }, function () {
                main_callback(false);
            });
        };

        async.all =
        async.every = function (arr, iterator, main_callback) {
            async.eachOf(arr, function (x, _, callback) {
                iterator(x, function (v) {
                    if (!v) {
                        main_callback(false);
                        main_callback = noop;
                    }
                    callback();
                });
            }, function () {
                main_callback(true);
            });
        };

        async.sortBy = function (arr, iterator, callback) {
            async.map(arr, function (x, callback) {
                iterator(x, function (err, criteria) {
                    Iif (err) {
                        callback(err);
                    }
                    else {
                        callback(null, {value: x, criteria: criteria});
                    }
                });
            }, function (err, results) {
                Iif (err) {
                    return callback(err);
                }
                else {
                    callback(null, _map(results.sort(comparator), function (x) {
                        return x.value;
                    }));
                }

            });

            function comparator(left, right) {
                var a = left.criteria, b = right.criteria;
                return a < b ? -1 : a > b ? 1 : 0;
            }
        };

        async.auto = function (tasks, callback) {
            callback = _once(callback || noop);
            var keys = _keys(tasks);
            var remainingTasks = keys.length;
            if (!remainingTasks) {
                return callback(null);
            }

            var results = {};

            var listeners = [];
            function addListener(fn) {
                listeners.unshift(fn);
            }
            function removeListener(fn) {
                for (var i = 0; i < listeners.length; i += 1) {
                    if (listeners[i] === fn) {
                        listeners.splice(i, 1);
                        return;
                    }
                }
            }
            function taskComplete() {
                remainingTasks--;
                _arrayEach(listeners.slice(0), function (fn) {
                    fn();
                });
            }

            addListener(function () {
                if (!remainingTasks) {
                    callback(null, results);
                }
            });

            _arrayEach(keys, function (k) {
                var task = _isArray(tasks[k]) ? tasks[k]: [tasks[k]];
                function taskCallback(err) {
                    var args = _baseSlice(arguments, 1);
                    if (args.length <= 1) {
                        args = args[0];
                    }
                    if (err) {
                        var safeResults = {};
                        _arrayEach(_keys(results), function(rkey) {
                            safeResults[rkey] = results[rkey];
                        });
                        safeResults[k] = args;
                        callback(err, safeResults);
                    }
                    else {
                        results[k] = args;
                        async.setImmediate(taskComplete);
                    }
                }
                var requires = task.slice(0, Math.abs(task.length - 1)) || [];
                // prevent dead-locks
                var len = requires.length;
                var dep;
                while (len--) {
                    if (!(dep = tasks[requires[len]])) {
                        throw new Error('Has inexistant dependency');
                    }
                    if (_isArray(dep) && !!~dep.indexOf(k)) {
                        throw new Error('Has cyclic dependencies');
                    }
                }
                function ready() {
                    return _reduce(requires, function (a, x) {
                        return (a && results.hasOwnProperty(x));
                    }, true) && !results.hasOwnProperty(k);
                }
                if (ready()) {
                    task[task.length - 1](taskCallback, results);
                }
                else {
                    addListener(listener);
                }
                function listener() {
                    if (ready()) {
                        removeListener(listener);
                        task[task.length - 1](taskCallback, results);
                    }
                }
            });
        };

        async.retry = function(times, task, callback) {
            var DEFAULT_TIMES = 5;
            var attempts = [];
            // Use defaults if times not passed
            if (typeof times === 'function') {
                callback = task;
                task = times;
                times = DEFAULT_TIMES;
            }
            // Make sure times is a number
            times = parseInt(times, 10) || DEFAULT_TIMES;

            function wrappedTask(wrappedCallback, wrappedResults) {
                function retryAttempt(task, finalAttempt) {
                    return function(seriesCallback) {
                        task(function(err, result){
                            seriesCallback(!err || finalAttempt, {err: err, result: result});
                        }, wrappedResults);
                    };
                }

                while (times) {
                    attempts.push(retryAttempt(task, !(times-=1)));
                }
                async.series(attempts, function(done, data){
                    data = data[data.length - 1];
                    (wrappedCallback || callback)(data.err, data.result);
                });
            }

            // If a callback is passed, run this as a controll flow
            return callback ? wrappedTask() : wrappedTask;
        };

        async.waterfall = function (tasks, callback) {
            callback = _once(callback || noop);
            if (!_isArray(tasks)) {
              var err = new Error('First argument to waterfall must be an array of functions');
              return callback(err);
            }
            if (!tasks.length) {
                return callback();
            }
            function wrapIterator(iterator) {
                return function (err) {
                    if (err) {
                        callback.apply(null, arguments);
                    }
                    else {
                        var args = _baseSlice(arguments, 1);
                        var next = iterator.next();
                        if (next) {
                            args.push(wrapIterator(next));
                        }
                        else {
                            args.push(callback);
                        }
                        ensureAsync(iterator).apply(null, args);
                    }
                };
            }
            wrapIterator(async.iterator(tasks))();
        };

        function _parallel(eachfn, tasks, callback) {
            callback = callback || noop;
            var results = _isArrayLike(tasks) ? [] : {};

            eachfn(tasks, function (task, key, callback) {
                task(function (err) {
                    var args = _baseSlice(arguments, 1);
                    if (args.length <= 1) {
                        args = args[0];
                    }
                    results[key] = args;
                    callback(err);
                });
            }, function (err) {
                callback(err, results);
            });
        }

        async.parallel = function (tasks, callback) {
            _parallel(async.eachOf, tasks, callback);
        };

        async.parallelLimit = function(tasks, limit, callback) {
            _parallel(_eachOfLimit(limit), tasks, callback);
        };

        async.series = function (tasks, callback) {
            callback = callback || noop;
            var results = _isArrayLike(tasks) ? [] : {};

            async.eachOfSeries(tasks, function (task, key, callback) {
                task(function (err) {
                    var args = _baseSlice(arguments, 1);
                    if (args.length <= 1) {
                        args = args[0];
                    }
                    results[key] = args;
                    callback(err);
                });
            }, function (err) {
                callback(err, results);
            });
        };

        async.iterator = function (tasks) {
            function makeCallback(index) {
                function fn() {
                    if (tasks.length) {
                        tasks[index].apply(null, arguments);
                    }
                    return fn.next();
                }
                fn.next = function () {
                    return (index < tasks.length - 1) ? makeCallback(index + 1): null;
                };
                return fn;
            }
            return makeCallback(0);
        };

        async.apply = function (fn) {
            var args = _baseSlice(arguments, 1);
            return function () {
                return fn.apply(
                    null, args.concat(_baseSlice(arguments))
                );
            };
        };

        function _concat(eachfn, arr, fn, callback) {
            var result = [];
            eachfn(arr, function (x, index, cb) {
                fn(x, function (err, y) {
                    result = result.concat(y || []);
                    cb(err);
                });
            }, function (err) {
                callback(err, result);
            });
        }
        async.concat = doParallel(_concat);
        async.concatSeries = doSeries(_concat);

        async.whilst = function (test, iterator, callback) {
            if (test()) {
                iterator(function (err) {
                    Iif (err) {
                        return callback(err);
                    }
                    async.whilst(test, iterator, callback);
                });
            }
            else {
                callback(null);
            }
        };

        async.doWhilst = function (iterator, test, callback) {
            iterator(function (err) {
                Iif (err) {
                    return callback(err);
                }
                var args = _baseSlice(arguments, 1);
                if (test.apply(null, args)) {
                    async.doWhilst(iterator, test, callback);
                }
                else {
                    callback(null);
                }
            });
        };

        async.until = function (test, iterator, callback) {
            if (!test()) {
                iterator(function (err) {
                    Iif (err) {
                        return callback(err);
                    }
                    async.until(test, iterator, callback);
                });
            }
            else {
                callback(null);
            }
        };

        async.doUntil = function (iterator, test, callback) {
            iterator(function (err) {
                Iif (err) {
                    return callback(err);
                }
                var args = _baseSlice(arguments, 1);
                if (!test.apply(null, args)) {
                    async.doUntil(iterator, test, callback);
                }
                else {
                    callback(null);
                }
            });
        };

        function _queue(worker, concurrency, payload) {
            if (concurrency == null) {
                concurrency = 1;
            }
            else if(concurrency === 0) {
                throw new Error('Concurrency must not be zero');
            }
            function _insert(q, data, pos, callback) {
                if (callback != null && typeof callback !== "function") {
                    throw new Error("task callback must be a function");
                }
                q.started = true;
                if (!_isArray(data)) {
                    data = [data];
                }
                if(data.length === 0 && q.idle()) {
                    // call drain immediately if there are no tasks
                    return async.setImmediate(function() {
                       q.drain();
                    });
                }
                _arrayEach(data, function(task) {
                    var item = {
                        data: task,
                        callback: callback || noop
                    };

                    if (pos) {
                      q.tasks.unshift(item);
                    } else {
                      q.tasks.push(item);
                    }

                    if (q.tasks.length === q.concurrency) {
                        q.saturated();
                    }
                    async.setImmediate(q.process);
                });
            }

            var workers = 0;
            var q = {
                tasks: [],
                concurrency: concurrency,
                saturated: noop,
                empty: noop,
                drain: noop,
                started: false,
                paused: false,
                push: function (data, callback) {
                    _insert(q, data, false, callback);
                },
                kill: function () {
                    q.drain = noop;
                    q.tasks = [];
                },
                unshift: function (data, callback) {
                    _insert(q, data, true, callback);
                },
                process: function () {
                    if (!q.paused && workers < q.concurrency && q.tasks.length) {
                        var tasks = payload ?
                            q.tasks.splice(0, payload) :
                            q.tasks.splice(0, q.tasks.length);

                        var data = _map(tasks, function (task) {
                            return task.data;
                        });

                        if (q.tasks.length === 0) {
                            q.empty();
                        }
                        workers += 1;
                        var cb = only_once(next);
                        worker(data, cb);
                    }

                    function next() {
                        workers -= 1;
                        var args = arguments;
                        _arrayEach(tasks, function (task) {
                            task.callback.apply(task, args);
                        });
                        if (q.tasks.length + workers === 0) {
                            q.drain();
                        }
                        q.process();
                    }
                },
                length: function () {
                    return q.tasks.length;
                },
                running: function () {
                    return workers;
                },
                idle: function() {
                    return q.tasks.length + workers === 0;
                },
                pause: function () {
                    q.paused = true;
                },
                resume: function () {
                    if (q.paused === false) { return; }
                    q.paused = false;
                    var resumeCount = Math.min(q.concurrency, q.tasks.length);
                    // Need to call q.process once per concurrent
                    // worker to preserve full concurrency after pause
                    for (var w = 1; w <= resumeCount; w++) {
                        async.setImmediate(q.process);
                    }
                }
            };
            return q;
        }

        async.queue = function (worker, concurrency) {
            var q = _queue(function (items, cb) {
                worker(items[0], cb);
            }, concurrency, 1);

            return q;
        };

        async.priorityQueue = function (worker, concurrency) {

            function _compareTasks(a, b){
                return a.priority - b.priority;
            }

            function _binarySearch(sequence, item, compare) {
              var beg = -1,
                  end = sequence.length - 1;
              while (beg < end) {
                  var mid = beg + ((end - beg + 1) >>> 1);
                  if (compare(item, sequence[mid]) >= 0) {
                      beg = mid;
                  } else {
                      end = mid - 1;
                  }
              }
              return beg;
            }

            function _insert(q, data, priority, callback) {
                Iif (callback != null && typeof callback !== "function") {
                    throw new Error("task callback must be a function");
                }
                q.started = true;
                Eif (!_isArray(data)) {
                    data = [data];
                }
                Iif(data.length === 0) {
                    // call drain immediately if there are no tasks
                    return async.setImmediate(function() {
                        q.drain();
                    });
                }
                _arrayEach(data, function(task) {
                    var item = {
                        data: task,
                        priority: priority,
                        callback: typeof callback === 'function' ? callback : noop
                    };

                    q.tasks.splice(_binarySearch(q.tasks, item, _compareTasks) + 1, 0, item);

                    if (q.tasks.length === q.concurrency) {
                        q.saturated();
                    }
                    async.setImmediate(q.process);
                });
            }

            // Start with a normal queue
            var q = async.queue(worker, concurrency);

            // Override push to accept second parameter representing priority
            q.push = function (data, priority, callback) {
                _insert(q, data, priority, callback);
            };

            // Remove unshift function
            delete q.unshift;

            return q;
        };

        async.cargo = function (worker, payload) {
            return _queue(worker, 1, payload);
        };

        function _console_fn(name) {
            return function (fn) {
                var args = _baseSlice(arguments, 1);
                fn.apply(null, args.concat([function (err) {
                    var args = _baseSlice(arguments, 1);
                    Eif (typeof console !== 'undefined') {
                        if (err) {
                            Eif (console.error) {
                                console.error(err);
                            }
                        }
                        else Eif (console[name]) {
                            _arrayEach(args, function (x) {
                                console[name](x);
                            });
                        }
                    }
                }]));
            };
        }
        async.log = _console_fn('log');
        async.dir = _console_fn('dir');
        /*async.info = _console_fn('info');
        async.warn = _console_fn('warn');
        async.error = _console_fn('error');*/

        async.memoize = function (fn, hasher) {
            var memo = {};
            var queues = {};
            hasher = hasher || function (x) {
                return x;
            };
            function memoized() {
                var args = _baseSlice(arguments);
                var callback = args.pop();
                var key = hasher.apply(null, args);
                if (key in memo) {
                    async.nextTick(function () {
                        callback.apply(null, memo[key]);
                    });
                }
                else if (key in queues) {
                    queues[key].push(callback);
                }
                else {
                    queues[key] = [callback];
                    fn.apply(null, args.concat([function () {
                        memo[key] = _baseSlice(arguments);
                        var q = queues[key];
                        delete queues[key];
                        for (var i = 0, l = q.length; i < l; i++) {
                          q[i].apply(null, arguments);
                        }
                    }]));
                }
            }
            memoized.memo = memo;
            memoized.unmemoized = fn;
            return memoized;
        };

        async.unmemoize = function (fn) {
          return function () {
            return (fn.unmemoized || fn).apply(null, arguments);
          };
        };

        async.times = function (count, iterator, callback) {
            var counter = [];
            for (var i = 0; i < count; i++) {
                counter.push(i);
            }
            return async.map(counter, iterator, callback);
        };

        async.timesSeries = function (count, iterator, callback) {
            var counter = [];
            for (var i = 0; i < count; i++) {
                counter.push(i);
            }
            return async.mapSeries(counter, iterator, callback);
        };

        async.seq = function (/* functions... */) {
            var fns = arguments;
            return function () {
                var that = this;
                var args = _baseSlice(arguments);

                var callback = args.slice(-1)[0];
                if (typeof callback == 'function') {
                    args.pop();
                } else {
                    callback = noop;
                }

                async.reduce(fns, args, function (newargs, fn, cb) {
                    fn.apply(that, newargs.concat([function () {
                        var err = arguments[0];
                        var nextargs = _baseSlice(arguments, 1);
                        cb(err, nextargs);
                    }]));
                },
                function (err, results) {
                    callback.apply(that, [err].concat(results));
                });
            };
        };

        async.compose = function (/* functions... */) {
          return async.seq.apply(null, Array.prototype.reverse.call(arguments));
        };

        function _applyEach(eachfn, fns /*args...*/) {
            function go() {
                var that = this;
                var args = _baseSlice(arguments);
                var callback = args.pop();
                return eachfn(fns, function (fn, _, cb) {
                    fn.apply(that, args.concat([cb]));
                },
                callback);
            }
            if (arguments.length > 2) {
                var args = _baseSlice(arguments, 2);
                return go.apply(this, args);
            }
            else {
                return go;
            }
        }

        async.applyEach = function (/*fns, args...*/) {
            var args = _baseSlice(arguments);
            return _applyEach.apply(null, [async.eachOf].concat(args));
        };
        async.applyEachSeries = function (/*fns, args...*/) {
            var args = _baseSlice(arguments);
            return _applyEach.apply(null, [async.eachOfSeries].concat(args));
        };

        async.forever = function (fn, callback) {
            var done = only_once(callback || noop);
            var task = ensureAsync(fn);
            function next(err) {
                if (err) {
                    return done(err);
                }
                task(next);
            }
            next();
        };

        function ensureAsync(fn) {
            return function (/*...args, callback*/) {
                var args = _baseSlice(arguments);
                var callback = args.pop();
                args.push(function () {
                    var innerArgs = arguments;
                    if (sync) {
                        async.setImmediate(function () {
                            callback.apply(null, innerArgs);
                        });
                    } else {
                        callback.apply(null, innerArgs);
                    }
                });
                var sync = true;
                fn.apply(this, args);
                sync = false;
            };
        }

        async.ensureAsync = ensureAsync;

        // Node.js
        Eif (typeof module !== 'undefined' && module.exports) {
            module.exports = async;
        }
        // AMD / RequireJS
        else if (typeof define !== 'undefined' && define.amd) {
            define([], function () {
                return async;
            });
        }
        // included directly via <script> tag
        else {
            root.async = async;
        }

    }());