| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 340b3be commit 2296b67
22 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -99,6 +99,15 @@ Tests whether `name` and `expected` are part of a raised warning. | |||
| 99 | 99 | ||
| 100 | 100 | Checks if `pathname` exists | |
| 101 | 101 | ||
| 102 | + ### fires(promise, [error], [timeoutMs]) | ||
| 103 | + * promise [<Promise] | ||
| 104 | + * error [<String] default = 'timeout' | ||
| 105 | + * timeoutMs [<Number] default = 100 | ||
| 106 | + | ||
| 107 | + Returns a new promise that will propagate `promise` resolution or rejection if | ||
| 108 | + that happens within the `timeoutMs` timespan, or rejects with `error` as | ||
| 109 | + a reason otherwise. | ||
| 110 | + | ||
| 102 | 111 | ### fixturesDir | |
| 103 | 112 | * return [<String>] | |
| 104 | 113 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -814,6 +814,32 @@ function restoreWritable(name) { | |||
| 814 | 814 | delete process[name].writeTimes; | |
| 815 | 815 | } | |
| 816 | 816 | ||
| 817 | + function onResolvedOrRejected(promise, callback) { | ||
| 818 | + return promise.then((result) => { | ||
| 819 | + callback(); | ||
| 820 | + return result; | ||
| 821 | + }, (error) => { | ||
| 822 | + callback(); | ||
| 823 | + throw error; | ||
| 824 | + }); | ||
| 825 | + } | ||
| 826 | + | ||
| 827 | + function timeoutPromise(error, timeoutMs) { | ||
| 828 | + let clearCallback = null; | ||
| 829 | + let done = false; | ||
| 830 | + const promise = onResolvedOrRejected(new Promise((resolve, reject) => { | ||
| 831 | + const timeout = setTimeout(() => reject(error), timeoutMs); | ||
| 832 | + clearCallback = () => { | ||
| 833 | + if (done) | ||
| 834 | + return; | ||
| 835 | + clearTimeout(timeout); | ||
| 836 | + resolve(); | ||
| 837 | + }; | ||
| 838 | + }), () => done = true); | ||
| 839 | + promise.clear = clearCallback; | ||
| 840 | + return promise; | ||
| 841 | + } | ||
| 842 | + | ||
| 817 | 843 | exports.hijackStdout = hijackStdWritable.bind(null, 'stdout'); | |
| 818 | 844 | exports.hijackStderr = hijackStdWritable.bind(null, 'stderr'); | |
| 819 | 845 | exports.restoreStdout = restoreWritable.bind(null, 'stdout'); | |
@@ -827,3 +853,19 @@ exports.firstInvalidFD = function firstInvalidFD() { | |||
| 827 | 853 | } catch (e) {} | |
| 828 | 854 | return fd; | |
| 829 | 855 | }; | |
| 856 | + | ||
| 857 | + exports.fires = function fires(promise, error, timeoutMs) { | ||
| 858 | + if (!timeoutMs && util.isNumber(error)) { | ||
| 859 | + timeoutMs = error; | ||
| 860 | + error = null; | ||
| 861 | + } | ||
| 862 | + if (!error) | ||
| 863 | + error = 'timeout'; | ||
| 864 | + if (!timeoutMs) | ||
| 865 | + timeoutMs = 100; | ||
| 866 | + const timeout = timeoutPromise(error, timeoutMs); | ||
| 867 | + return Promise.race([ | ||
| 868 | + onResolvedOrRejected(promise, () => timeout.clear()), | ||
| 869 | + timeout | ||
| 870 | + ]); | ||
| 871 | + }; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments