|
1 | | -// Hprose for HTML5 v2.0.27 |
| 1 | +// Hprose for HTML5 v2.0.28 |
2 | 2 | // Copyright (c) 2008-2016 http://hprose.com |
3 | 3 | // Hprose is freely distributable under the MIT license. |
4 | 4 | // For all details and documentation: |
@@ -1309,27 +1309,36 @@ hprose.global = ( |
1309 | 1309 | return isGenerator(constructor.prototype); |
1310 | 1310 | } |
1311 | 1311 |
|
| 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 | + |
1312 | 1335 | function thunkToPromise(fn) { |
1313 | 1336 | if (isGeneratorFunction(fn) || isGenerator(fn)) { |
1314 | 1337 | return co(fn); |
1315 | 1338 | } |
1316 | 1339 | var thisArg = (function() { return this; })(); |
1317 | 1340 | 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)); |
1333 | 1342 | return future; |
1334 | 1343 | } |
1335 | 1344 |
|
@@ -1359,29 +1368,15 @@ hprose.global = ( |
1359 | 1368 | function promisify(fn) { |
1360 | 1369 | return function() { |
1361 | 1370 | 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)); |
1378 | 1373 | try { |
1379 | 1374 | fn.apply(this, args); |
1380 | 1375 | } |
1381 | 1376 | catch (err) { |
1382 | | - results.reject(err); |
| 1377 | + future.reject(err); |
1383 | 1378 | } |
1384 | | - return results; |
| 1379 | + return future; |
1385 | 1380 | }; |
1386 | 1381 | } |
1387 | 1382 |
|
|
0 commit comments