diff options
| author | akiyamn | 2023-09-24 23:22:21 +1000 |
|---|---|---|
| committer | akiyamn | 2023-09-24 23:22:21 +1000 |
| commit | 4e87195739f2a5d9a05451b48773c8afdc680765 (patch) | |
| tree | 9cba501844a4a11dcbdffc4050ed8189561c55ed /node_modules/get-source/impl/SyncPromise.js | |
| download | price-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.tar.gz price-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.zip | |
Initial commit (by create-cloudflare CLI)
Diffstat (limited to 'node_modules/get-source/impl/SyncPromise.js')
| -rw-r--r-- | node_modules/get-source/impl/SyncPromise.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/node_modules/get-source/impl/SyncPromise.js b/node_modules/get-source/impl/SyncPromise.js new file mode 100644 index 0000000..1412532 --- /dev/null +++ b/node_modules/get-source/impl/SyncPromise.js @@ -0,0 +1,51 @@ +"use strict"; + +/* ------------------------------------------------------------------------ */ + +module.exports = class SyncPromise { + + constructor (fn) { + try { + fn ( + x => { this.setValue (x, false) }, // resolve + x => { this.setValue (x, true) } // reject + ) + } catch (e) { + this.setValue (e, true) + } + } + + setValue (x, rejected) { + this.val = (x instanceof SyncPromise) ? x.val : x + this.rejected = rejected || ((x instanceof SyncPromise) ? x.rejected : false) + } + + static valueFrom (x) { + if (x instanceof SyncPromise) { + if (x.rejected) throw x.val + else return x.val + } else { + return x + } + } + + then (fn) { + try { if (!this.rejected) return SyncPromise.resolve (fn (this.val)) } + catch (e) { return SyncPromise.reject (e) } + return this + } + + catch (fn) { + try { if (this.rejected) return SyncPromise.resolve (fn (this.val)) } + catch (e) { return SyncPromise.reject (e) } + return this + } + + static resolve (x) { + return new SyncPromise (resolve => { resolve (x) }) + } + + static reject (x) { + return new SyncPromise ((_, reject) => { reject (x) }) + } +}
\ No newline at end of file |
