-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsubpromise.php
92 lines (87 loc) · 2.7 KB
/
subpromise.php
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* This file is part of the Promise package, a StreamCommon open software project.
*
* @copyright (c) 2019-2020 StreamCommon
* @see https://github.com/streamcommon/promise
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
use Streamcommon\Promise\Promise;
use Streamcommon\Promise\WaitInterface;
if (file_exists(__DIR__ . '/../../../autoload.php')) {
require __DIR__ . '/../../../autoload.php';
} elseif (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
} else {
throw new \RuntimeException('File autoload.php not exists');
}
#############################################################
#############################################################
$promise = Promise::create(function (callable $resolve) {
$promise = Promise::create(function (callable $resolve) {
$resolve(41);
});
$promise->then(function ($value) use ($resolve) {
$resolve($value);
});
if ($promise instanceof WaitInterface) {
$promise->wait();
}
});
$promise2 = $promise->then(function ($value) {
return $value + 1;
});
$promise->then(function ($value) {
echo $value . ' === 41' . PHP_EOL;
});
$promise2->then(function ($value) {
echo $value . ' === 42' . PHP_EOL;
});
if ($promise instanceof WaitInterface) {
$promise->wait();
}
#############################################################
#############################################################
$promise = Promise::create(function (callable $resolve) {
$resolve(Promise::create(function (callable $resolve) {
$resolve(42);
}));
});
$promise2 = $promise->then(function ($value) {
return $value + 1;
});
$promise->then(function ($value) {
echo $value . ' === 42' . PHP_EOL;
});
$promise2->then(function ($value) {
echo $value . ' === 43' . PHP_EOL;
});
if ($promise instanceof WaitInterface) {
$promise->wait();
}
#############################################################
#############################################################
$promise = Promise::create(function (callable $resolve) {
$resolve(43);
});
$promise2 = $promise->then(function ($value) {
// HAVE BIG PROBLEM
return Promise::create(function (callable $resolve) use ($value) {
$resolve($value + 1);
})->then(function ($value) {
return $value + 1;
});
});
$promise->then(function ($value) {
echo $value . ' === 43' . PHP_EOL;
});
$promise2->then(function ($value) {
echo $value . ' === 45' . PHP_EOL;
});
if ($promise instanceof WaitInterface) {
$promise->wait();
}
#############################################################