Skip to content

Commit 84a07f9

Browse files
committed
Support async callbacks.
1 parent 022c67b commit 84a07f9

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

example-async/lib/js/src/AA.js

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example-async/src/AA.res

+15
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ let testFetch =
9292
testFetch->addTest1("https://www.google.com/sdkjdkghdsg")
9393
testFetch->addTest1("https://www.google.comsdkjdkghdsg")
9494

95+
//
96+
//
97+
// Callbacks
98+
let withCallback =
99+
@async
100+
(. ()) => {
101+
let callback = @async (. x) => x + 1
102+
callback
103+
}
104+
105+
let testWithCallback =
106+
@async (. ()) => Js.log2("callback returned", @await (@await withCallback(.))(. 3))
107+
108+
testWithCallback->addTest
109+
95110
//
96111
//
97112
// Run tests

jscomp/core/js_dump.ml

+8-9
Original file line numberDiff line numberDiff line change
@@ -392,24 +392,23 @@ and pp_function ~return_unit ~async ~is_method cxt (f : P.t) ~fn_state
392392
match fn_state with
393393
| Is_return ->
394394
return_sp f;
395-
P.string f L.function_;
395+
P.string f (L.function_async ~async);
396396
P.space f;
397397
param_body b
398398
| No_name { single_arg } ->
399399
(* see # 1692, add a paren for annoymous function for safety *)
400400
P.cond_paren_group f (not single_arg) 1 (fun _ ->
401-
P.string f L.function_;
402-
P.space f;
401+
P.string f (L.function_async ~async);
402+
P.space f;
403403
param_body b)
404404
| Name_non_top x ->
405405
ignore (pp_var_assign inner_cxt f x : cxt);
406-
P.string f L.function_;
406+
P.string f (L.function_async ~async);
407407
P.space f;
408408
param_body b;
409409
semi f
410410
| Name_top x ->
411-
if async then P.string f "async ";
412-
P.string f L.function_;
411+
P.string f (L.function_async ~async);
413412
P.space f;
414413
ignore (Ext_pp_scope.ident inner_cxt f x : cxt);
415414
param_body b)
@@ -425,7 +424,7 @@ and pp_function ~return_unit ~async ~is_method cxt (f : P.t) ~fn_state
425424
| Name_non_top name | Name_top name ->
426425
ignore (pp_var_assign inner_cxt f name : cxt));
427426
P.string f L.lparen;
428-
P.string f L.function_;
427+
P.string f (L.function_async ~async);
429428
pp_paren_params inner_cxt f lexical;
430429
P.brace_vgroup f 0 (fun _ ->
431430
return_sp f;
@@ -859,9 +858,9 @@ and expression_desc cxt ~(level : int) f x : cxt =
859858
else
860859
P.brace_vgroup f 1 (fun _ -> property_name_and_value_list cxt f lst))
861860
| Await e ->
861+
P.cond_paren_group f (level > 13) 1 (fun _ ->
862862
P.string f "await ";
863-
let cxt = expression ~level cxt f e in
864-
cxt
863+
expression ~level:13 cxt f e)
865864

866865
and property_name_and_value_list cxt f (l : J.property_map) =
867866
iter_lst cxt f l

jscomp/core/js_dump_lit.ml

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
let function_ = "function"
2626

27+
let function_async ~async = if async then "async function" else "function"
28+
2729
let var = "var" (* should be able to switch to [let] easily*)
2830

2931
let return = "return"

0 commit comments

Comments
 (0)