Skip to content
This repository

Added applySync method #231

Closed
wants to merge 3 commits into from

2 participants

Sean Fridman Caolan McMahon
Sean Fridman

Implements the applySync method discussed in caolan/async#208

Caolan McMahon
Owner

tests are failing for this, please re-open if still required.

Caolan McMahon caolan closed this March 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
55  README.md
Source Rendered
@@ -97,6 +97,7 @@ So far its been tested in IE6, IE7, IE8, FF3.6 and Chrome 5. Usage:
97 97
 * [auto](#auto)
98 98
 * [iterator](#iterator)
99 99
 * [apply](#apply)
  100
+* [applySync](#applySync)
100 101
 * [nextTick](#nextTick)
101 102
 * [times](#times)
102 103
 * [timesSeries](#timesSeries)
@@ -1081,6 +1082,60 @@ three
1081 1082
 
1082 1083
 ---------------------------------------
1083 1084
 
  1085
+<a name="applySync" />
  1086
+### applySync(function, arguments..)
  1087
+
  1088
+The same as apply, only designed to be used as shorthand for creating
  1089
+continuations for _synchronous_ tasks.
  1090
+
  1091
+__Arguments__
  1092
+
  1093
+* function - The function you want to eventually apply all arguments to.
  1094
+* arguments... - Any number of arguments to automatically apply when the
  1095
+  continuation is called.
  1096
+
  1097
+
  1098
+__Example__
  1099
+
  1100
+```js
  1101
+// using applySync
  1102
+
  1103
+async.series([
  1104
+    async.applySync(fs.writeFileSync, 'testfile1', 'test1')
  1105
+    // ...
  1106
+]);
  1107
+
  1108
+
  1109
+// the same process without using applySync
  1110
+
  1111
+async.series([
  1112
+    function(callback){
  1113
+        try {
  1114
+            fs.writeFileSync('testfile1', 'test1');
  1115
+            callback();
  1116
+        }
  1117
+        catch (err) {
  1118
+            callback(err);
  1119
+        }
  1120
+    }
  1121
+    / ...
  1122
+]);
  1123
+```
  1124
+
  1125
+The continuation function expects a callback as its last argument.
  1126
+
  1127
+```js
  1128
+node> var fn = async.applySync(sys.puts, 'one');
  1129
+node> fn('two', 'three');
  1130
+TypeError
  1131
+node> fn('two', 'three', function() {});
  1132
+one
  1133
+two
  1134
+three
  1135
+```
  1136
+
  1137
+---------------------------------------
  1138
+
1084 1139
 <a name="nextTick" />
1085 1140
 ### nextTick(callback)
1086 1141
 
17  lib/async.js
@@ -583,6 +583,23 @@
583 583
         };
584 584
     };
585 585
 
  586
+    async.applySync = function (fn) {
  587
+        var args = Array.prototype.slice.call(arguments, 1);
  588
+        return function () {
  589
+            var argsTask = Array.prototype.slice.call(arguments);
  590
+            var callback = argsTask.pop();
  591
+            var result;
  592
+            try {
  593
+                result = fn.apply(null, args.concat(argsTask));
  594
+                async.nextTick(callback);
  595
+                return result;
  596
+            }
  597
+            catch (err) {
  598
+                callback(err);
  599
+            }
  600
+        };
  601
+    };
  602
+
586 603
     var _concat = function (eachfn, arr, fn, callback) {
587 604
         var r = [];
588 605
         eachfn(arr, function (x, cb) {
25  test/test-async.js
@@ -1165,6 +1165,31 @@ exports['apply'] = function(test){
1165 1165
     test.done();
1166 1166
 };
1167 1167
 
  1168
+exports['applySync'] = function(test){
  1169
+    test.expect(7);
  1170
+    var fn = function(){
  1171
+        test.same(Array.prototype.slice.call(arguments), [1,2,3,4])
  1172
+    };
  1173
+    var callback = function() {};
  1174
+    async.applySync(fn, 1, 2, 3, 4)(callback);
  1175
+    async.applySync(fn, 1, 2, 3)(4, callback);
  1176
+    async.applySync(fn, 1, 2)(3, 4, callback);
  1177
+    async.applySync(fn, 1)(2, 3, 4, callback);
  1178
+    async.applySync(fn)(1, 2, 3, 4, callback);
  1179
+    test.equals(
  1180
+        async.applySync(function(name){return 'hello ' + name}, 'world')(),
  1181
+        'hello world'
  1182
+    );
  1183
+
  1184
+    async.series([
  1185
+        async.applySync(function() {}),
  1186
+        async.applySync(function() {}),
  1187
+        async.applySync(function() {})
  1188
+    ], function(err, result){
  1189
+        test.ok(true);
  1190
+        test.done();
  1191
+    });
  1192
+};
1168 1193
 
1169 1194
 // generates tests for console functions such as async.log
1170 1195
 var console_fn_tests = function(name){
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.