A minimalistic implementation of Promises for PHP.
Install the DEVirion plugin and start your server. This will create a virions
folder in your server's root directory.
server_root
| -> plugins
| --> DEVirion.phar
| -> virions
- Download pre-compiled
.phar
files can be downloaded from poggit. - Place the pre-compiled
.phar
in thevirions
directory
Clone the repository via git git clone [email protected]:cooldogedev/libPromise.git
. This will create a libPromise
folder in your directory.
your_plugin
| -> src
| --> cooldogedev
| --> libPromise
- Place the
cooldogedev\libPromise
folder in yoursrc
directory.
$promise = new Promise(
function (): string {
return "Hello world";
}
);
$promise
->then(fn(string $response) => var_dump($response))
->settle();
$promise = new Promise(
function (): string {
$person = [
"name" => "john",
"lastName" => "smith",
"age" => 40
];
return json_encode($person);
}
);
$promise
->then(fn(string $response): array => json_decode($response, true))
->then(fn(array $response) => var_dump($response))
->settle();
$pool = new PromisePool($this);
$promise = new ThreadedPromise(
function (): ThreadedPromise {
return new ThreadedPromise(fn(string $response) => var_dump("a nested promise"));
},
function (ThreadedPromise $promise): void {
/**
* You can convert the ThreadedPromise to a regular Promise via @link ThreadedPromise::asPromise()
*/
$promise->settle();
}
);
$pool->addPromise($promise);
$pool = new PromisePool($this);
$promise = new ThreadedPromise(
function (): string {
$request = Internet::getURL("https://api.coinbase.com/v2/currencies");
return $request->getBody();
}
);
$promise
->then(fn(string $response): array => json_decode($response, true))
->then(fn(array $response) => var_dump($response))
->catch(fn(PromiseError $error) => var_dump($error->getMessage()));
$pool->addPromise($promise);
$pool = new PromisePool($this);
$promise = new ThreadedPromise(
function (): string {
$request = Internet::getURL("https://v2.jokeapi.dev/joke/Any");
return $request->getBody();
},
function (array $response): void {
foreach ($this->getServer()->getOnlinePlayers() as $onlinePlayer) {
$onlinePlayer->sendMessage($response["setup"]);
$onlinePlayer->sendMessage($response["delivery"]);
}
}
);
$promise
->then(fn(string $response): array => json_decode($response, true))
->catch(fn(PromiseError $error) => var_dump($error->getMessage()));
$pool->addPromise($promise);