A promise to eventually produce a result of type A. This promise can either be fulfilled or rejected.
Any number of promises can be chained after this one.
- Promise[A] tag^
Fulfill the promise.
- value: A
Reject the promise.
Chain a promise after this one.
When this promise is fulfilled, the result of type A is passed to the fulfill function, generating in an intermediate result of type B. This is then used to fulfill the next promise in the chain.
If there is no fulfill function, or if the fulfill function raises an error, then the next promise in the chain will be rejected.
If this promise is rejected, this step's reject function is called with no input, generating an intermediate result of type B which is used to fulfill the next promise in the chain.
If there is no reject function, of if the reject function raises an error, then the next promise in the chain will be rejected.
- Promise[B] tag
Chain a promise after this one and unwrap the promise returned from this one.
flatten_next is a companion to
next. It operates in an identical
fashion except for the type of the fulfilled promise. Whereas
a function that returns a type
flatten_next takes a function that
flatten_next valuable given that next could take a
B that is of
a type like
Promise[String]? Let's start with some code to demonstrate the
problem that arises when returning
Let's say we have a library for accessing the GitHub REST API:
And we want to use this promise based API to look up the title of an issue.
flatten_next, we could attempt to do the following using
actor Main new create(env: Env) => let repo: Promise[Repository] = GitHub("my token").get_repo("ponylang/ponyc") // // do something with the repo once the promise is fulfilled // in our case, get the issue // let issue = Promise[Promise[Issue]] = repo.next[Promise[Issue]](FetchIssue~apply(1)) // once we get the issue, print the title issue.next[None](PrintIssueTitle~apply(env.out)) primitive FetchIssue fun apply(number: I64, repo: Repository): Promise[Issue] => repo.get_issue(number) primitive PrintIssueTitle fun apply(out: OutStream, issue: Promise[Issue]) => // O NO! We can't print the title // We don't have an issue, we have a promise for an issue
Take a look at what happens in the example, when we get to
PrintIssueTitle, we can't print anything because we "don't have anything".
In order to print the issue title, we need an
Issue not a
We could solve this by doing something like this:
That will work, however, it is kind of awful. When looking at:
it can be hard to follow what is going on. We can only tell what is
happening because we gave
PrintIssueTitle a very misleading name; it
doesn't print an issue title.
flatten_next addresses the problem of "we want the
Issue, not the
flatten_next takes an intermediate promise and
unwraps it into the fulfilled type. You get to write your promise chain
without having to worry about intermediate promises.
Updated to use
flatten_next, our API example becomes:
actor Main new create(env: Env) => let repo: Promise[Repository] = GitHub("my token").get_repo("ponylang/ponyc") let issue = Promise[Issue] = repo.flatten_next[Issue](FetchIssue~apply(1)) issue.next[None](PrintIssueTitle~apply(env.out)) primitive FetchIssue fun apply(number: I64, repo: Repository): Promise[Issue] => repo.get_issue(number) primitive PrintIssueTitle fun apply(out: OutStream, issue: Issue) => out.print(issue.title())
Issue, is no longer a
Promise[Promise[Issue]]. By using
flatten_next, we have a much more manageable
Other than unwrapping promises for you,
flatten_next otherwise acts the
next so all the same rules apply to fulfillment and rejection.
- Promise[B] tag
Add two promises into one promise that returns the result of both when they are fulfilled. If either of the promises is rejected then the new promise is also rejected.
- p: Promise[B] tag
- Promise[(A , B)] tag
Create a promise that is fulfilled when the receiver and all promises in the given iterator are fulfilled. If the receiver or any promise in the sequence is rejected then the new promise is also rejected.
p2 with an existing promise,
Return a promise that is fulfilled when either promise is fulfilled, resulting in a tuple of its value and the other promise.
- p: Promise[A] tag
Reject the promise after the given expiration in nanoseconds.
- expiration: U64 val
- None val