Skip to Content
Raku学习手册
book

Raku学习手册

by brian d foy
October 2020
Beginner to intermediate
499 pages
9h 13m
Chinese
China Electric Power Press Ltd.
Content preview from Raku学习手册
348
18
18.3 Promise
Promise
是在一段时间以后生成某个结果的一些代码,而且这段时间可能不短。它会
调度另一个线程完成这个工作,而程序的其余部分仍继续运行。这些正是
Perl 6
并发
性的基础,它们会为你完成大部分艰巨的工作。
每个
Promise
都有一个状态,可能在等待运行、正在运行或者已经完成。它的完成
方式决定了它的状态:一个
Promise
成功时状态就是
Kept
,失败时状态为
Broken
如果正在工作,状态则是
Planned
简单的
Promise
是一个计时器。
.in
方法会建立一个
Promise
,它会保持到你指定的
秒数:
my $five-seconds-from-now = Promise.in: 5;
loop {
sleep 1;
put "Promise status is: ", $five-seconds-from-now.status;
}
开始时,
Promise
的状态是
Planned
。(大约)
5
秒后,这个
Promise
转换为
Kept
这时你就能知道已经过去了
5
秒:
Promise status is: Planned
Promise status is: Planned
Promise status is: Planned
Promise status is: Planned
Promise status is: Kept
Promise status is: Kept
...
不需要持续不断地检查
Promise
。可以使用
.then
指定转换为
kept
状态时要运行的 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

可编程网络自动化

可编程网络自动化

Jason Edelman, Scott S. Lowe, Matt Oswalt
程序员学数据结构

程序员学数据结构

Posts & Telecom Press, William Smith
R深度学习权威指南

R深度学习权威指南

Posts & Telecom Press, Joshua F. Wiley
AI工程

AI工程

Chip Huyen

Publisher Resources

ISBN: 9787519848002