summaryrefslogtreecommitdiff
path: root/lib/asyncify.js
diff options
context:
space:
mode:
authorGraeme Yeates <yeatesgraeme@gmail.com>2015-12-23 15:50:39 -0500
committerGraeme Yeates <yeatesgraeme@gmail.com>2015-12-29 16:48:48 -0500
commit18e61d4f07f48604601f2effdbe2a7e188d14d4a (patch)
treed4761428f57ec80816bea109124fd836cdaf20a6 /lib/asyncify.js
parent7127b67f94a22247c36bf40e4f2685912e0f80e9 (diff)
downloadasync-18e61d4f07f48604601f2effdbe2a7e188d14d4a.tar.gz
[WIP] modularization (#984)
Diffstat (limited to 'lib/asyncify.js')
-rw-r--r--lib/asyncify.js26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/asyncify.js b/lib/asyncify.js
new file mode 100644
index 0000000..6fdc62d
--- /dev/null
+++ b/lib/asyncify.js
@@ -0,0 +1,26 @@
+'use strict';
+
+import isObject from 'lodash/lang/isObject';
+import restParam from 'lodash/function/restParam';
+
+export default function asyncify(func) {
+ return restParam(function (args) {
+ var callback = args.pop();
+ var result;
+ try {
+ result = func.apply(this, args);
+ } catch (e) {
+ return callback(e);
+ }
+ // if result is Promise object
+ if (isObject(result) && typeof result.then === 'function') {
+ result.then(function(value) {
+ callback(null, value);
+ })['catch'](function(err) {
+ callback(err.message ? err : new Error(err));
+ });
+ } else {
+ callback(null, result);
+ }
+ });
+}