File: promise.t

package info (click to toggle)
libjavascript-quickjs-perl 0.21-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,180 kB
  • sloc: ansic: 72,822; javascript: 7,743; perl: 1,065; makefile: 353; sh: 108
file content (58 lines) | stat: -rw-r--r-- 929 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Test::FailWarnings;
use Test::Deep;

use JavaScript::QuickJS;

my @out;
sub faux_print {
    push @out, [@_];
}

my $js = JavaScript::QuickJS->new();

faux_print('start');
my $p1 = $js->eval('Promise.resolve(123)');
faux_print('created promise');

my $p2 = $p1->then( sub {
    faux_print("first then", @_);
    return 234;
} );
faux_print('called then');

my $p3 = $p2->then( sub {
    faux_print("second then", @_);
    die 345;
} );

$p3->catch( sub {
    faux_print("catch", @_);
    return 456;
} )->finally( sub {
    faux_print("finally", @_);
} );

faux_print('before await');
$js->await();

cmp_deeply(
    \@out,
    [
        ['start'],
        ['created promise'],
        ['called then'],
        ['before await'],
        ['first then', 123],
        ['second then', 234],
        ['catch', re(345)],
        ['finally'],
    ],
);

done_testing;