summaryrefslogtreecommitdiff
path: root/lib/ensureAsync.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ensureAsync.js')
-rw-r--r--lib/ensureAsync.js24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/ensureAsync.js b/lib/ensureAsync.js
new file mode 100644
index 0000000..66de26b
--- /dev/null
+++ b/lib/ensureAsync.js
@@ -0,0 +1,24 @@
+'use strict';
+
+import restParam from 'lodash/function/restParam';
+
+import setImmediate from './internal/setImmediate';
+
+export default function ensureAsync(fn) {
+ return restParam(function (args) {
+ var callback = args.pop();
+ args.push(function () {
+ var innerArgs = arguments;
+ if (sync) {
+ setImmediate(function () {
+ callback.apply(null, innerArgs);
+ });
+ } else {
+ callback.apply(null, innerArgs);
+ }
+ });
+ var sync = true;
+ fn.apply(this, args);
+ sync = false;
+ });
+}