Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use v6;
- class Promise {
- enum STATE <PENDING FULFILLED REJECTED>;
- has STATE $!state;
- has %!do-actions;
- method state {
- $!state;
- }
- method fulfill($val!) {
- if $!state != STATE::FULFILLED {
- $!state = STATE::FULFILLED;
- %!do-actions<fulfill> = $val;
- }
- }
- method reject($reason!) {
- if $!state != STATE::REJECTED {
- $!state = STATE::REJECTED;
- %!do-actions<reject> = $reason;
- }
- }
- method then($on-fulfill?, $on-reject?) {
- if $!state == STATE::PENDING {
- %!do-actions<fulfill>.push: { $on-fulfill } if $on-fulfill;
- %!do-actions<reject>.push: { $on-reject } if $on-reject;
- } elsif %!state == STATE::REJECTED {
- %!do-actions<reject>.map: { $^x.() };
- } else {
- %!do-actions<fulfill>.map: { $^x.() };
- }
- return self;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment