@@ -1125,12 +1125,73 @@ module Helpers =
11251125 let callInfo = Fable.CallInfo.Create( args = [ e ])
11261126 makeIdentExpr " str" |> makeCall None Fable.String callInfo
11271127
1128- /// Transform return statements to wrap their values with await
1129- let wrapReturnWithAwait ( body : Statement list ) : Statement list =
1128+ /// Transform return statements to wrap their values with await.
1129+ /// Recursively traverses nested control flow (if/else, match, try, for, while, with)
1130+ /// to ensure ALL return statements in async functions get awaited.
1131+ let rec wrapReturnWithAwait ( body : Statement list ) : Statement list =
11301132 body
11311133 |> List.map ( fun stmt ->
11321134 match stmt with
1135+ | Statement.Return { Value = Some( Await _) } -> stmt // Already awaited
11331136 | Statement.Return { Value = Some value } -> Statement.return' ( Await value)
1137+ | Statement.If ifStmt ->
1138+ Statement.if' (
1139+ ifStmt.Test,
1140+ wrapReturnWithAwait ifStmt.Body,
1141+ wrapReturnWithAwait ifStmt.Else,
1142+ ?loc = ifStmt.Loc
1143+ )
1144+ | Statement.Match matchStmt ->
1145+ let cases =
1146+ matchStmt.Cases
1147+ |> List.map ( fun case ->
1148+ MatchCase.matchCase ( case.Pattern, wrapReturnWithAwait case.Body, ?guard = case.Guard)
1149+ )
1150+
1151+ Statement.match' ( matchStmt.Subject, cases, ?loc = matchStmt.Loc)
1152+ | Statement.Try tryStmt ->
1153+ let handlers =
1154+ tryStmt.Handlers
1155+ |> List.map ( fun h -> { h with Body = wrapReturnWithAwait h.Body })
1156+
1157+ Statement.try' (
1158+ wrapReturnWithAwait tryStmt.Body,
1159+ handlers = handlers,
1160+ orElse = wrapReturnWithAwait tryStmt.OrElse,
1161+ finalBody = wrapReturnWithAwait tryStmt.FinalBody,
1162+ ?loc = tryStmt.Loc
1163+ )
1164+ | Statement.For forStmt ->
1165+ Statement.for' (
1166+ forStmt.Target,
1167+ forStmt.Iterator,
1168+ body = wrapReturnWithAwait forStmt.Body,
1169+ orelse = wrapReturnWithAwait forStmt.Else,
1170+ ?typeComment = forStmt.TypeComment
1171+ )
1172+ | Statement.AsyncFor asyncForStmt ->
1173+ AsyncFor(
1174+ AsyncFor.asyncFor (
1175+ asyncForStmt.Target,
1176+ asyncForStmt.Iterator,
1177+ wrapReturnWithAwait asyncForStmt.Body,
1178+ orelse = wrapReturnWithAwait asyncForStmt.Else,
1179+ ?typeComment = asyncForStmt.TypeComment
1180+ )
1181+ )
1182+ | Statement.While whileStmt ->
1183+ Statement.while' (
1184+ whileStmt.Test,
1185+ wrapReturnWithAwait whileStmt.Body,
1186+ orelse = wrapReturnWithAwait whileStmt.Else,
1187+ ?loc = whileStmt.Loc
1188+ )
1189+ | Statement.With withStmt ->
1190+ Statement.with' (
1191+ withStmt.Items,
1192+ body = wrapReturnWithAwait withStmt.Body,
1193+ ?typeComment = withStmt.TypeComment
1194+ )
11341195 | other -> other
11351196 )
11361197
0 commit comments