Skip to content

Commit 715bdcc

Browse files
committed
Improved thunk & promisify
1 parent 181424d commit 715bdcc

7 files changed

Lines changed: 65 additions & 75 deletions

File tree

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"author": "Ma Bingyao <andot@hprose.com>",
33
"name": "hprose-html5",
4-
"version": "2.0.27",
4+
"version": "2.0.28",
55
"description": "Hprose is a High Performance Remote Object Service Engine.",
66
"keywords": [
77
"hprose",

dist/hprose-html5.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/hprose-html5.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/hprose-html5.src.js

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Hprose for HTML5 v2.0.27
1+
// Hprose for HTML5 v2.0.28
22
// Copyright (c) 2008-2016 http://hprose.com
33
// Hprose is freely distributable under the MIT license.
44
// For all details and documentation:
@@ -1309,27 +1309,36 @@ hprose.global = (
13091309
return isGenerator(constructor.prototype);
13101310
}
13111311

1312+
function getThunkCallback(future) {
1313+
return function(err, res) {
1314+
if (err instanceof Error) {
1315+
return future.reject(err);
1316+
}
1317+
if (arguments.length < 2) {
1318+
return future.resolve(err);
1319+
}
1320+
if (err === null || err === undefined) {
1321+
res = Array.slice(arguments, 1);
1322+
}
1323+
else {
1324+
res = Array.slice(arguments, 0);
1325+
}
1326+
if (res.length == 1) {
1327+
future.resolve(res[0]);
1328+
}
1329+
else {
1330+
future.resolve(res);
1331+
}
1332+
};
1333+
}
1334+
13121335
function thunkToPromise(fn) {
13131336
if (isGeneratorFunction(fn) || isGenerator(fn)) {
13141337
return co(fn);
13151338
}
13161339
var thisArg = (function() { return this; })();
13171340
var future = new Future();
1318-
fn.call(thisArg, function(err, res) {
1319-
if (arguments.length < 2) {
1320-
if (err instanceof Error) {
1321-
return future.reject(err);
1322-
}
1323-
return future.resolve(err);
1324-
}
1325-
if (err) {
1326-
return future.reject(err);
1327-
}
1328-
if (arguments.length > 2) {
1329-
res = Array.slice(arguments, 1);
1330-
}
1331-
future.resolve(res);
1332-
});
1341+
fn.call(thisArg, getThunkCallback(future));
13331342
return future;
13341343
}
13351344

@@ -1359,29 +1368,15 @@ hprose.global = (
13591368
function promisify(fn) {
13601369
return function() {
13611370
var args = Array.slice(arguments, 0);
1362-
var results = new Future();
1363-
args.push(function(err, res) {
1364-
if (arguments.length < 2) {
1365-
if (err instanceof Error) {
1366-
return results.reject(err);
1367-
}
1368-
return results.resolve(err);
1369-
}
1370-
if (err) {
1371-
return results.reject(err);
1372-
}
1373-
if (arguments.length > 2) {
1374-
res = Array.slice(arguments, 1);
1375-
}
1376-
results.resolve(res);
1377-
});
1371+
var future = new Future();
1372+
args.push(getThunkCallback(future));
13781373
try {
13791374
fn.apply(this, args);
13801375
}
13811376
catch (err) {
1382-
results.reject(err);
1377+
future.reject(err);
13831378
}
1384-
return results;
1379+
return future;
13851380
};
13861381
}
13871382

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "hprose-html5",
33
"filename": "hprose-html5.js",
4-
"version": "2.0.27",
4+
"version": "2.0.28",
55
"description": "Hprose is a High Performance Remote Object Service Engine.",
66
"homepage": "https://github.com/andot/hprose",
77
"keywords": [

src/CopyRight.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Hprose for HTML5 v2.0.27
1+
// Hprose for HTML5 v2.0.28
22
// Copyright (c) 2008-2016 http://hprose.com
33
// Hprose is freely distributable under the MIT license.
44
// For all details and documentation:

src/Future.js

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -224,27 +224,36 @@
224224
return isGenerator(constructor.prototype);
225225
}
226226

227+
function getThunkCallback(future) {
228+
return function(err, res) {
229+
if (err instanceof Error) {
230+
return future.reject(err);
231+
}
232+
if (arguments.length < 2) {
233+
return future.resolve(err);
234+
}
235+
if (err === null || err === undefined) {
236+
res = Array.slice(arguments, 1);
237+
}
238+
else {
239+
res = Array.slice(arguments, 0);
240+
}
241+
if (res.length == 1) {
242+
future.resolve(res[0]);
243+
}
244+
else {
245+
future.resolve(res);
246+
}
247+
};
248+
}
249+
227250
function thunkToPromise(fn) {
228251
if (isGeneratorFunction(fn) || isGenerator(fn)) {
229252
return co(fn);
230253
}
231254
var thisArg = (function() { return this; })();
232255
var future = new Future();
233-
fn.call(thisArg, function(err, res) {
234-
if (arguments.length < 2) {
235-
if (err instanceof Error) {
236-
return future.reject(err);
237-
}
238-
return future.resolve(err);
239-
}
240-
if (err) {
241-
return future.reject(err);
242-
}
243-
if (arguments.length > 2) {
244-
res = Array.slice(arguments, 1);
245-
}
246-
future.resolve(res);
247-
});
256+
fn.call(thisArg, getThunkCallback(future));
248257
return future;
249258
}
250259

@@ -274,29 +283,15 @@
274283
function promisify(fn) {
275284
return function() {
276285
var args = Array.slice(arguments, 0);
277-
var results = new Future();
278-
args.push(function(err, res) {
279-
if (arguments.length < 2) {
280-
if (err instanceof Error) {
281-
return results.reject(err);
282-
}
283-
return results.resolve(err);
284-
}
285-
if (err) {
286-
return results.reject(err);
287-
}
288-
if (arguments.length > 2) {
289-
res = Array.slice(arguments, 1);
290-
}
291-
results.resolve(res);
292-
});
286+
var future = new Future();
287+
args.push(getThunkCallback(future));
293288
try {
294289
fn.apply(this, args);
295290
}
296291
catch (err) {
297-
results.reject(err);
292+
future.reject(err);
298293
}
299-
return results;
294+
return future;
300295
};
301296
}
302297

0 commit comments

Comments
 (0)