1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Haxe で非同期処理のコールバックはどう書けば?

Posted at

Java みたいにコールバックの interface を定義しても匿名クラスが使えない?みたいだし、JavaScript みたいに function を Dynamic で渡すのも型安全じゃない。。。。

こうすればいいのか!

// 呼ばれる方
public function sendRequest(path:String, 
	onSuccess:String->Void, onError:String->Void) : Void {
	// 成功
	onSuccess("yeah!");
	// 失敗
	onError("oops…");
}


// 呼ぶ方
sendRequest("http://hoge", 
	function(data:String):Void {
		// 成功した時、呼ばれる
		trace(data);
	},
	function(err:String):Void {
		// 失敗した時、呼ばれる
		trace(err);
	}
)

これなら呼ぶ方で function の定義を間違えると、コンパイル時にエラーにしてくれる。

Proxy - Haxe http://haxe.org/doc/proxy にそんな感じで書いてあった。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?