gpt4 book ai didi

dart - 异步的优缺点是什么,何时使用和何时不使用它以及回调的其他替代方案是什么?

转载 作者:行者123 更新时间:2023-12-05 00:51:23 25 4
gpt4 key购买 nike

回调或异步方法或其他选项

回调瘟疫的解决方案是“await”和“async”或更具体的“dart:async”库。

现在,异步的成本是多少?
我们什么时候不应该使用它们?
其他选择是什么?

下面是一个编码错误的非聚合物自定义元素,它在桌面环境中就像一个消息框。它给了我更少的大括号和括号,但要求调用者也是异步的或使用“show().then((v){print(v);});”图案。我应该避免这样的模式吗?
回调更好吗?还是有更聪明的方法?

轮询版本

import 'dart:html';
import 'dart:async';
void init(){
document.registerElement('list-modal',ListModal);
}
class ListModal extends HtmlElement{
ListModal.created():super.created();
String _modal_returns="";
void set modal_returns(String v){
///use the modal_returns setter to
///implement a custom behaviour for
///the return value of the show method
///within the callback you can pass on calling append .
_modal_returns=v;
}

factory ListModal(){
var e = new Element.tag('list-modal');
e.style..backgroundColor="olive"
..position="absolute"
..margin="auto"
..top="50%"
..verticalAlign="middle";
var close_b = new DivElement();
close_b.text = "X";
close_b.style..right="0"
..top="0"
..margin="0"
..verticalAlign="none"
..backgroundColor="blue"
..position="absolute";
close_b.onClick.listen((_){
e.hide();
});
e.append(close_b,(_)=>e.hide());
e.hide();
return e;
}
@override
ListModal append(
HtmlElement e,
[Function clickHandler=null]
){
super.append(e);
if(clickHandler!=null) {
e.onClick.listen(clickHandler);
}else{
e.onClick.listen((_){
this.hide();
_modal_returns = e.text;
});
}
return this;
}
Future<String> show() async{
_modal_returns = '';
this.hidden=false;
await wait_for_input();
print(_modal_returns);
return _modal_returns;
}
wait_for_input() async{
while(_modal_returns=="" && !this.hidden){
await delay();
}
}
void hide(){
this.hidden=true;
}
Future delay() async{
return new Future.delayed(
new Duration(milliseconds: 100));
}
}

非轮询版本

响应 Günter Zöchbauer 的智慧(避免轮询),发布使用完成器的版本。一如既往地感谢 Günter Zöchbauer:
import 'dart:html';
import 'dart:async';
void init(){
document.registerElement('list-modal',ListModal);
}
class ListModal extends HtmlElement{
ListModal.created():super.created();
String _modal_returns="";
Completer _completer;
void set modal_returns(String v){
///use the modal_returns setter to
///implement a custom behaviour for
///the return value of the show method.
///Use this setter within the callback for
///append. Always call hide() after
///setting modal_returns.
_modal_returns=v;
}

factory ListModal(){
var e = new Element.tag('list-modal');
e.style..backgroundColor="olive"
..position="absolute"
..margin="auto"
..top="50%"
..verticalAlign="middle";
var close_b = new DivElement();
close_b.text = "X";
close_b.style..right="0"
..top="0"
..margin="0"
..verticalAlign="none"
..backgroundColor="blue"
..position="absolute";
close_b.onClick.listen((_){
e.hide();
});
e.append(close_b,(_){e.hide();});
e.hide();
return e;
}
@override
ListModal append(
HtmlElement e,
[Function clickHandler=null]
){
super.append(e);
if(clickHandler!=null) {
e.onClick.listen(clickHandler);
}else{
e.onClick.listen((_){
_modal_returns = e.text;
this.hide();
});
}
return this;
}
Future<String> show() async{
_modal_returns = '';
_completer = new Completer();
this.hidden=false;
return _completer.future;
}
void hide(){
hidden=true;
_completer?.complete(_modal_returns);
_completer=null;
}
}

最佳答案

通常,是否应该使用异步是毫无疑问的。通常人们会尽量避免它。一旦您调用异步 API,您的代码就会异步,您无法选择是否需要。
在某些情况下,故意将异步执行设为异步。例如,将大型计算分成较小的 block ,以免使事件队列无法处理。

在服务器端,有几个 API 函数允许在同步和异步版本之间进行选择。关于何时使用哪个进行了广泛的讨论。我会查找并添加链接。

使用async的缺点/await而不是 .then()应该是最小的。

  • 带有 async 的最小 Dart SDK 版本/await支持是 1.9.1
  • 在第一次执行代码之前,VM 需要进行一些额外的重写,但这通常可以忽略不计。

  • 您的代码似乎进行轮询。
    wait_for_input() async {
    while(_modal_returns=="" && !this.hidden){
    await delay();
    }
    }

    如果可能,应该避免这种情况。
    最好让 modal 自己管理其隐藏状态(例如通过添加 hide() 方法),然后它不必轮询它是否从外部隐藏。

    关于dart - 异步的优缺点是什么,何时使用和何时不使用它以及回调的其他替代方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35007238/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com