@makano/rew 1.4.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
|
|
1
1
|
const emitter = require('./emitter');
|
2
|
+
const { wait } = require('./wait');
|
2
3
|
|
3
4
|
function future(callback, timeout = 0, defData = null) {
|
4
5
|
const listener = emitter();
|
@@ -19,6 +20,8 @@ function future(callback, timeout = 0, defData = null) {
|
|
19
20
|
resolve: (data) => listener.emit('resolve', data),
|
20
21
|
reject: (data) => listener.emit('reject', data),
|
21
22
|
wait: async () => await promise,
|
23
|
+
sync: (fn) => wait(async () => fn ? (await fn(promise)) : await promise),
|
24
|
+
_isfuture: true
|
22
25
|
};
|
23
26
|
};
|
24
27
|
|
@@ -3,11 +3,21 @@ const deasync = require("deasync");
|
|
3
3
|
|
4
4
|
module.exports.wait = (...args) => {
|
5
5
|
const fn = args.shift();
|
6
|
-
if(typeof fn !== "function") throw new TypeError("The first argument must be a function to use wait.");
|
6
|
+
if(typeof fn !== "function" && typeof fn?.then != "function" && !fn?._isfuture) throw new TypeError("The first argument must be a function, future or a Promise to use wait.");
|
7
7
|
const df = deasync(async (cb) => {
|
8
|
-
fn(...args)
|
9
|
-
|
10
|
-
|
8
|
+
let result = typeof fn == "function" ? fn(...args) : fn;
|
9
|
+
|
10
|
+
if(result._isfuture){
|
11
|
+
result = result.wait();
|
12
|
+
}
|
13
|
+
|
14
|
+
if(typeof result.then == "function"){
|
15
|
+
result
|
16
|
+
.then(d => cb(null, d))
|
17
|
+
.catch(d => cb(d));
|
18
|
+
} else {
|
19
|
+
cb(null, result);
|
20
|
+
}
|
11
21
|
});
|
12
22
|
return df();
|
13
23
|
}
|