DART函数,嵌套/内部函数变量 [英] DART function, nested / inner functions variable

查看:671
本文介绍了DART函数,嵌套/内部函数变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码作为匿名函数(也是函数字面量或lambda抽象),用于 DART邮件

 电子邮件(){
...
emailTransport.send )
.then((success)=> print('Email sent!$ success'))
.catchError((e)=&
}

这个工作正常,但我需要替换打印返回如下:

 电子邮件(){
...
emailTransport.send信封)
.then((success)=> return'Email sent!$ success')
.catchError((e)=> return'Error occurredured:$ e');
}

但未能成功返回。



我尝试了下面的代码,但失败了。

  b ... 
var msg;
...
emailTransport.send(envelope)
.then((success)=> msg ='Email sent!$ success')
.catchError((e) => msg ='错误发生:$ e');

return msg;
}

但是msg仍为NULL!


这是因为 return 在你的函数中不是正在执行你的代码的 Futures 链的一部分。您的函数立即返回;并且emailTransport.send方法尚未运行。



您的函数需要返回未来;我不认为有任何办法阻止,并等待结果(如果有,你可能不想这样做!)。



您可能想要这样做:

 未来的电子邮件(){
...
return emailTransport.send(envelope)
.then((success)=>'Email sent!$ success')
.catchError((e)=>'Error occurredured:$ e');
}

然后,调用该函数的任何东西都需要链接到未来:

 电子邮件()
.then(msg => print(msg));

根据评论进行修改



你调用的原始方法是异步的,所以返回 Future (例如,将来会完成的东西)。要对这个值做任何事情,你需要链接更多的代码到结束(结果,也将返回一个未来,因为它不能运行直到第一个完成)。



您可以分配到链接函数中的变量,例如

  email()。then((m)=> msg = m); 

但是,这只会在异步操作完成后执行,因此不会立即可用在这行代码后(这是你的原始代码示例中的错误)。如果你想做一些有价值的事情,你真的需要链接到未来:

  email()
.then(doSomeOtherThing)

doSomeOtherThing(String msg){
//在这里做更多处理
}

如果你不熟悉Futures,Dart网站上有一篇可能值得一读的文章:使用基于Future的API



这与NodeJS的工作原理非常相似, 块,而是需要在异步/长时间运行工作之后完成的工作有效地在回调中被标记到结束,并且运行时在一个大循环中处理队列中的下一个。这里有一些更多的信息:事件循环和飞镖


I've the below code as anonymous function (also function literal or lambda abstraction), which is used in DART mailer

email(){ 
   ...
   emailTransport.send(envelope)
   .then((success) => print('Email sent! $success'))
   .catchError((e) => print('Error occured: $e'));
}

this worked fine, but I need to replace the "print" by a "return" to be like below:

email(){ 
   ...
   emailTransport.send(envelope)
   .then((success) => return 'Email sent! $success')
   .catchError((e) => return 'Error occured: $e');
}

but failed, the return had not been recognized!

I tried the below code, but failed too.

email(){ 
   ...
   var msg;
   ...
   emailTransport.send(envelope)
   .then((success) => msg = 'Email sent! $success')
   .catchError((e) => msg = 'Error occured: $e');

 return msg;
}

But the "msg" remained NULL!

any thoughts.

解决方案

This is because the return in your function is not part of the chain of Futures that are executing your code. Your function is returning immediately; and the emailTransport.send method has not run yet.

Your function needs to return a Future; I don't think there is any way to "block" and wait for the result (and if there was, you probably wouldn't want to do this!).

You probably want to do something like this:

Future email() {
  ...
  return emailTransport.send(envelope)
   .then((success) => 'Email sent! $success')
   .catchError((e) => 'Error occured: $e');
}

And then, anything calling the function will need to also chain onto the future:

email()
  .then(msg => print(msg));

Edit: based on comment

The original method you're calling is asynchronous, so returns a Future (eg. something that will complete in the future). To do anything with this value, you need to "chain" more code onto the end (which as a result, will also return a Future, because it can't run until the first is completed).

You can assign to a variable inside a chained function, eg.

email().then((m) => msg = m);

However, this will only be executed once the async operation has completed, so it will not be immediately available after this line of code (this was ths mistake in your original code sample). If you want to do something with the value, you really need to chain that into the future too:

email()
  .then(doSomeOtherThing)

doSomeOtherThing(String msg) {
  // Do more processing here
}

If you're not familiar with Futures, there's an article on the Dart site that might be worth reading: Use Future-Based APIs

It's very similar to how NodeJS works, nothing should ever "block", but instead, work that needs to be done after async/long-running work is effectively in a callback that gets tagged onto the end, and the runtime sits in a big loop processing what's next on the queue. There's a little more info on this here: The Event Loop and Dart.

这篇关于DART函数,嵌套/内部函数变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆