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 181424d commit 715bdcc
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 75 deletions.
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"author": "Ma Bingyao <andot@hprose.com>",
"name": "hprose-html5",
"version": "2.0.27",
"version": "2.0.28",
"description": "Hprose is a High Performance Remote Object Service Engine.",
"keywords": [
"hprose",
Expand Down
8 changes: 4 additions & 4 deletions dist/hprose-html5.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

63 changes: 29 additions & 34 deletions dist/hprose-html5.src.js
@@ -1,4 +1,4 @@
// Hprose for HTML5 v2.0.27
// Hprose for HTML5 v2.0.28
// Copyright (c) 2008-2016 http://hprose.com
// Hprose is freely distributable under the MIT license.
// For all details and documentation:
Expand Down Expand Up @@ -1309,27 +1309,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 @@ -1359,29 +1368,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
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "hprose-html5",
"filename": "hprose-html5.js",
"version": "2.0.27",
"version": "2.0.28",
"description": "Hprose is a High Performance Remote Object Service Engine.",
"homepage": "https://github.com/andot/hprose",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion src/CopyRight.js
@@ -1,4 +1,4 @@
// Hprose for HTML5 v2.0.27
// Hprose for HTML5 v2.0.28
// 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
Expand Up @@ -224,27 +224,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 @@ -274,29 +283,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

0 comments on commit 715bdcc

Please sign in to comment.