Skip to content

Commit

Permalink
Improved thunk & promisify
Browse files Browse the repository at this point in the history
  • Loading branch information
andot committed Nov 19, 2016
1 parent 8c12191 commit 4cba9d1
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 110 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Ma Bingyao <[email protected]>",
"name": "hprose",
"version": "2.0.25",
"version": "2.0.26",
"description": "Hprose is a High Performance Remote Object Service Engine.",
"keywords": [
"hprose",
Expand Down
24 changes: 11 additions & 13 deletions dist/hprose.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/hprose.min.js

Large diffs are not rendered by default.

63 changes: 29 additions & 34 deletions dist/hprose.src.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Hprose for JavaScript v2.0.25
// Hprose for JavaScript v2.0.26
// Copyright (c) 2008-2016 http://hprose.com
// Hprose is freely distributable under the MIT license.
// For all details and documentation:
Expand Down Expand Up @@ -1948,27 +1948,36 @@ hprose.global = (
return isGenerator(constructor.prototype);
}

function getThunkCallback(future) {
return function(err, res) {
if (err instanceof Error) {
return future.reject(err);
}
if (arguments.length < 2) {
return future.resolve(err);
}
if (err === null || err === undefined) {
res = Array.slice(arguments, 1);
}
else {
res = Array.slice(arguments, 0);
}
if (res.length == 1) {
future.resolve(res[0]);
}
else {
future.resolve(res);
}
};
}

function thunkToPromise(fn) {
if (isGeneratorFunction(fn) || isGenerator(fn)) {
return co(fn);
}
var thisArg = (function() { return this; })();
var future = new Future();
fn.call(thisArg, function(err, res) {
if (arguments.length < 2) {
if (err instanceof Error) {
return future.reject(err);
}
return future.resolve(err);
}
if (err) {
return future.reject(err);
}
if (arguments.length > 2) {
res = Array.slice(arguments, 1);
}
future.resolve(res);
});
fn.call(thisArg, getThunkCallback(future));
return future;
}

Expand Down Expand Up @@ -1998,29 +2007,15 @@ hprose.global = (
function promisify(fn) {
return function() {
var args = Array.slice(arguments, 0);
var results = new Future();
args.push(function(err, res) {
if (arguments.length < 2) {
if (err instanceof Error) {
return results.reject(err);
}
return results.resolve(err);
}
if (err) {
return results.reject(err);
}
if (arguments.length > 2) {
res = Array.slice(arguments, 1);
}
results.resolve(res);
});
var future = new Future();
args.push(getThunkCallback(future));
try {
fn.apply(this, args);
}
catch (err) {
results.reject(err);
future.reject(err);
}
return results;
return future;
};
}

Expand Down
24 changes: 11 additions & 13 deletions example/hprose.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hprose-js",
"filename": "hprose.js",
"version": "2.0.25",
"version": "2.0.26",
"description": "Hprose is a High Performance Remote Object Service Engine.",
"homepage": "https://github.com/hprose",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/CopyRight.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Hprose for JavaScript v2.0.25
// Hprose for JavaScript v2.0.26
// Copyright (c) 2008-2016 http://hprose.com
// Hprose is freely distributable under the MIT license.
// For all details and documentation:
Expand Down
61 changes: 28 additions & 33 deletions src/Future.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,27 +227,36 @@
return isGenerator(constructor.prototype);
}

function getThunkCallback(future) {
return function(err, res) {
if (err instanceof Error) {
return future.reject(err);
}
if (arguments.length < 2) {
return future.resolve(err);
}
if (err === null || err === undefined) {
res = Array.slice(arguments, 1);
}
else {
res = Array.slice(arguments, 0);
}
if (res.length == 1) {
future.resolve(res[0]);
}
else {
future.resolve(res);
}
};
}

function thunkToPromise(fn) {
if (isGeneratorFunction(fn) || isGenerator(fn)) {
return co(fn);
}
var thisArg = (function() { return this; })();
var future = new Future();
fn.call(thisArg, function(err, res) {
if (arguments.length < 2) {
if (err instanceof Error) {
return future.reject(err);
}
return future.resolve(err);
}
if (err) {
return future.reject(err);
}
if (arguments.length > 2) {
res = Array.slice(arguments, 1);
}
future.resolve(res);
});
fn.call(thisArg, getThunkCallback(future));
return future;
}

Expand Down Expand Up @@ -277,29 +286,15 @@
function promisify(fn) {
return function() {
var args = Array.slice(arguments, 0);
var results = new Future();
args.push(function(err, res) {
if (arguments.length < 2) {
if (err instanceof Error) {
return results.reject(err);
}
return results.resolve(err);
}
if (err) {
return results.reject(err);
}
if (arguments.length > 2) {
res = Array.slice(arguments, 1);
}
results.resolve(res);
});
var future = new Future();
args.push(getThunkCallback(future));
try {
fn.apply(this, args);
}
catch (err) {
results.reject(err);
future.reject(err);
}
return results;
return future;
};
}

Expand Down
24 changes: 11 additions & 13 deletions test/hprose.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4cba9d1

Please sign in to comment.