Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

後方で宣言した関数がasyncFnだったとき、その関数が正しく実行できない問題 #1758

Open
kujirahand opened this issue Oct 17, 2024 · 2 comments

Comments

@kujirahand
Copy link
Owner

https://nadesi.com/cgi/kaizen3/index.php?m=thread&threadid=92 より

v3.6.22でも似た感じのが発生しています
ローカル変数がundefinedになる

//再現 (v3.6.22)
!インデント構文
0.1秒後には
 Aとは変数 = 「A」
 関数A
 Aを表示 //→undefined

●関数A
 Bとは変数 = 「B」
 関数B
 Bを表示 //→undefined

●関数B
 もし0ならば
  (1/30)秒待つ //(待たない)

ちなみに関数Bで待った場合「B」は表示されます

@kujirahand kujirahand changed the title 未定義の関数がasyncFnだったとき、関数の定義前でその関数を使うと挙動がおかしくなる問題 後方で宣言した関数がasyncFnだったとき、その関数が正しく実行できない問題 Oct 17, 2024
@kujirahand
Copy link
Owner Author

kujirahand commented Oct 17, 2024

上記サンプルの「には」構文を、関数定義の末尾に持っていくと問題なく実行できます。
「には」構文(無名関数)を宣言する時点で、その関数が非同期かどうかが分かっていないのが問題となっています。

理由ですが、ある関数Cで参照した関数Aから呼び出している関数Bが非同期(asyncFn)だったとき、その関数を呼び出す関数Cを非同期で実行する必要があります。

根本的な解決案としては、なでしこのユーザー関数をすべて非同期関数(asyncFn)であると仮定するしかありません。
実行速度が遅くなるデメリットがありますが。

そこで下記の2つの解決作があります。

  • (1) なでしこの関数定義もasyncを明示できるような機能をつける
  • (2) なでしこのユーザー関数を問答無用でasyncにしてしまう

@kujirahand
Copy link
Owner Author

kujirahand commented Oct 17, 2024

ちょっと悩むけど、ユーザーにとって、変換され実行される時の関数がasyncかどうかはあまり問題ではないと思うので、上記(2)を採用するのが濃厚かも…。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant