-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (87 loc) · 2.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.awaitSync = exports.deasync = void 0;
/// <reference types="./missing-types" />
const binding_node_1 = require("./build/Release/binding.node");
function loopWhile(pred) {
while (pred()) {
process._tickCallback();
if (pred())
(0, binding_node_1.run)();
}
}
/**
* Determine whether the value is a Promise.
*
* @see https://promisesaplus.com/
*/
function isThenable(value) {
return typeof value.then === "function";
}
// Can't use enum as async-to-sync breaks the control flow analyzing.
const Pending = 0;
const Fulfilled = 1;
const Rejected = 2;
/**
* Generic wrapper of async function with conventional API signature
* `function (...args, (error, result) => {})`.
*
* Returns `result` and throws `error` as exception if not null.
*
* @param fn the original callback style function
* @return the wrapped function
*/
function deasync(fn) {
return function (...args) {
let state = Pending;
let resultOrError;
args.push((err, res) => {
if (err) {
resultOrError = err;
state = Rejected;
}
else {
resultOrError = res;
state = Fulfilled;
}
});
fn.apply(this, args);
loopWhile(() => state === Pending);
if (state === Rejected) {
throw resultOrError;
}
else {
return resultOrError;
}
};
}
exports.deasync = deasync;
/**
* Similar with the keyword `await` but synchronously.
*
* @param promise A Promise or any value to wait for
* @return Returns the fulfilled value of the promise, or the value itself if it's not a Promise.
*/
function awaitSync(promise) {
let state = Pending;
let resultOrError;
if (!isThenable(promise)) {
return promise;
}
promise.then(res => {
resultOrError = res;
state = Fulfilled;
}, err => {
resultOrError = err;
state = Rejected;
});
loopWhile(() => state === Pending);
if (state === Rejected) {
throw resultOrError;
}
else {
return resultOrError;
}
}
exports.awaitSync = awaitSync;
//# sourceMappingURL=index.js.map