aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-08-29 17:56:01 +0100
committerLouis Pilfold <louis@lpil.uk>2021-08-29 17:56:01 +0100
commit30e5e540ab4edf6582ae77f9440044e22d6d197e (patch)
tree11196f6e11fbd6d2de7840b99d3a250166f047c1 /src
parent6abcea012a67ab6b4eafc3401b0e1ca6a368e21a (diff)
downloadjavascript-30e5e540ab4edf6582ae77f9440044e22d6d197e.tar.gz
javascript-30e5e540ab4edf6582ae77f9440044e22d6d197e.zip
Promise rescue
Diffstat (limited to 'src')
-rw-r--r--src/ffi.js4
-rw-r--r--src/gleam/javascript/promise.gleam5
2 files changed, 9 insertions, 0 deletions
diff --git a/src/ffi.js b/src/ffi.js
index 450f5e3..d3ee8ce 100644
--- a/src/ffi.js
+++ b/src/ffi.js
@@ -94,3 +94,7 @@ export function map_promise(promise, fn) {
PromiseLayer.wrap(fn(PromiseLayer.unwrap(value)))
);
}
+
+export function rescue(promise, fn) {
+ return promise.catch((error) => fn(error));
+}
diff --git a/src/gleam/javascript/promise.gleam b/src/gleam/javascript/promise.gleam
index 8407c8c..3c7190c 100644
--- a/src/gleam/javascript/promise.gleam
+++ b/src/gleam/javascript/promise.gleam
@@ -1,3 +1,5 @@
+import gleam/dynamic.{Dynamic}
+
// TODO: docs
// TODO: labels
pub external type Promise(value)
@@ -5,6 +7,9 @@ pub external type Promise(value)
pub external fn resolve(value) -> Promise(value) =
"../../ffi.js" "resolve"
+pub external fn rescue(Promise(value), fn(Dynamic) -> value) -> Promise(value) =
+ "../../ffi.js" "rescue"
+
pub external fn then(Promise(a), fn(a) -> Promise(b)) -> Promise(b) =
"../../ffi.js" "then"