Skip to content

Commit dd80925

Browse files
Add doc comment for JS::Object#await
1 parent 5617b39 commit dd80925

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

ext/js/lib/js.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ def respond_to_missing?(sym, include_private)
8484
self[sym].typeof == "function"
8585
end
8686

87+
# Await a JavaScript Promise like `await` in JavaScript.
88+
# This method looks like a synchronous method, but it actually runs asynchronously using fibers.
89+
#
90+
# JS.eval("return new Promise((ok) => setTimeout(ok(42), 1000))").await # => 42 (after 1 second)
91+
# JS.global.fetch("https://example.com").await # => [object Response]
92+
# JS.eval("return 42").await # => 42
93+
# JS.eval("return new Promise((ok, err) => err(new Error())").await # => raises JS::Error
8794
def await
8895
# Promise.resolve wrap a value or flattens promise-like object and its thenable chain
8996
promise = JS.global[:Promise].resolve(self)

0 commit comments

Comments
 (0)