Skip to content

Commit b84aac4

Browse files
Rephrase JS::Object#await description and enhance examples
1 parent 985e43e commit b84aac4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

ext/js/lib/js.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ def respond_to_missing?(sym, include_private)
9696

9797
# Await a JavaScript Promise like `await` in JavaScript.
9898
# This method looks like a synchronous method, but it actually runs asynchronously using fibers.
99+
# In other words, the next line to the `await` call at Ruby source will be executed after the
100+
# promise will be resolved. However, it does not block JavaScript event loop, so the next line
101+
# to the `RubyVM.eval` or `RubyVM.evalAsync` (in the case when no `await` operator before the
102+
# call expression) at JavaScript source will be executed without waiting for the promise.
103+
#
104+
# The below example shows how the execution order goes. It goes in the order of "step N"
105+
#
106+
# # In JavaScript
107+
# const response = vm.evalAsync(`
108+
# puts "step 1"
109+
# JS.global.fetch("https://example.com").await
110+
# puts "step 3"
111+
# `) // => Promise
112+
# console.log("step 2")
113+
# await response
114+
# console.log("step 4")
115+
#
116+
# The below examples show typical usage in Ruby
99117
#
100118
# JS.eval("return new Promise((ok) => setTimeout(ok(42), 1000))").await # => 42 (after 1 second)
101119
# JS.global.fetch("https://example.com").await # => [object Response]

0 commit comments

Comments
 (0)