来源于个人理解的翻译。

创建一个 promise:

my $p = Promise.new;

可以打印运行 的Promise 状态:

my $p = Promise.new();

$p.then({say 'hello, world'});
say $p.status;

上面的promise创建好后, 当 $p 状态为 kept或broken 时, 会执行 then 里面的 匿名函数。

但是, 上面的$p状态总是为: Plannd。

所以, 那个 hello, world 总是不能打印。

那怎么办呢?

Promise 有两个 函数用于更改 promise状态:

. keep
. break

可以改成这样:

my $p = Promise.new();

$p.then({say 'hello, world'});
say $p.status; $p.keep;
say 'Result ~ '~$p.result;

结果如下:

C:\p6>perl6 scan_dir.p6
Planned
hello, world
Result ~ True C:\p6>

如果把 keep 改为 break的话, 也会让 匿名函数执行, 同时会引发一个常常:

my $p = Promise.new();

$p.then({say 'hello, world'});
say $p.status;
$p.break;
say 'Result ~ '~$p.result;

运行结果如下:

C:\p6>perl6 scan_dir.p6
Planned
hello, world
Tried to get the result of a broken Promise
in block <unit> at scan_dir.p6 line Original exception:
False
in block <unit> at scan_dir.p6 line C:\p6>

注意这里的匿名函数, 当 Promise 执行 kept或 broken 方法时, 会执行它, 最后 promise中的result方法会返回一个值, 这个返回值是传递给 keep 或 break的值或者是then返回了另一个新Promise

的result值(这个值是匿名函数的返回值):

my $p = Promise.new();

$p.then({say 'hello, world'});
say $p.status;
$p.keep('Return for Promise');
say 'Result ~ '~$p.result;
say $p.status;

结果:

C:\p6>perl6 scan_dir.p6
Planned
Result ~ Return for Promise
hello, world
Kept C:\p6>
my $p = Promise.new();

my $p1 = $p.then({ 'hello, world'});
say $p.status;
#$p.keep('Return for Promise');
$p.keep;
say 'Result ~ '~$p.result;
say $p.status;
say $p1.result;

结果:

C:\p6>perl6 scan_dir.p6
Planned
Result ~ True
Kept
hello, world C:\p6>

如果不传递参数执行 $p.keep 时,  匿名函数执行完后, 用 $p.result 会返回一个 True, 这个true 代理 原来的Promise(这里的$p)执行完毕, 状态为 kept。

这个 then 方法, 其实会自动创建一个新的Promise并返回这个Promise(这里的$p1), then方法中的匿名函数参数就是原来的Promise($p), 看如下代码:

my $p = Promise.new();

my $p1 = $p.then(-> $key { say $key.result});
#这里相当于创建了一个新的Promise并返回它
#key 其实就是 $p
$p.keep('This is $p');
#say $p.status;
sleep ;
say $p.status;
say $p1.status;

像一开始所说, Promise.new 创建一个 Promise 时, 默认是不会自动执行的,这时当我们 直接调用 $p.result 想获得 结果时, 程序会一直阻塞(因为我们没有 keep 或 bread 方法告诉这个 promise 让它运行):

my $p = Promise.new();

$p.then({say 'hello, world'});
say $p.status;
say 'Result ~ '~$p.result; #这里会一直阻塞
say 'Done'; #上面被阻塞 , 这里不会运行

那么, 我们能不能一开始创建 一个 promise 后, 让它自动去执行呢?

可以, 用 Promist.start() 方法即可。

最新文章

  1. AFNetworking 3.0 源码解读(五)之 AFURLSessionManager
  2. android——从零开始
  3. Qt 常用的功能
  4. HTML5学习之跨文档传输消息(七)
  5. svn提交时强制添加注释
  6. 如何给一个网卡配置多个虚拟ip
  7. hdu 5774 Where Amazing Happens
  8. [Everyday Mathematics]20150122
  9. Celery Flower监控,完美搞定
  10. Linq101-Partitioning
  11. [置顶] 【cocos2d-x入门实战】微信飞机大战之二:别急,先处理好CCScene和CCLayer的关系
  12. Windows下python安装MySQLdb
  13. 543. Diameter of Binary Tree
  14. TypeScript: Week Reflection
  15. 【bzoj 2916】[Poi1997]Monochromatic Triangles
  16. network is unreachable 网关PING不通解决办法
  17. 软件测试中Bug的生命周期以及Bug的严重等级
  18. Avalon Framework
  19. 一、spring boot 1.5.4入门(web+freemarker)
  20. 实现自己的ls命令

热门文章

  1. [LeetCode] Climbing Sairs
  2. 第209天:jQuery运动框架封装(二)
  3. 第136天:Web前端面试题总结(理论)
  4. Arrays.toString 如果传入的是对象 那么调用的是此对象的toString
  5. bzoj3517 翻硬币
  6. 转--- 秒杀多线程第六篇 经典线程同步 事件Event
  7. 【Java并发编程】之四:守护线程与线程阻塞的四种情况
  8. shell脚本学习—正则表达式
  9. javascript如何封装函数
  10. CodeForces - 955B(用char会超时。。。)