### Setup Project Dependencies Source: https://github.com/nunomaduro/pokio/blob/main/CONTRIBUTING.md Installs the development dependencies required for the Pokio project using Composer. This is a crucial step before making any code contributions. ```bash composer install ``` -------------------------------- ### Install Pokio using Composer Source: https://github.com/nunomaduro/pokio/blob/main/README.md Provides the Composer command to install the Pokio package. This is the standard method for adding PHP libraries to a project, ensuring compatibility and managing dependencies. ```bash composer require nunomaduro/pokio:^0.1 ``` -------------------------------- ### PHP Asynchronous API Example Source: https://github.com/nunomaduro/pokio/blob/main/README.md Demonstrates the usage of Pokio's async function to run tasks concurrently. It shows how to define asynchronous tasks, await their completion, and retrieve results. This example highlights the non-blocking nature of the API. ```php $promiseA = async(function () { sleep(2); return 'Task 1'; }); $promiseB = async(function () { sleep(2); return 'Task 2'; }); // just takes 2 seconds... [$resA, $resB] = await([$promiseA, $promiseB]); echo $resA; // Task 1 echo $resB; // Task 2 ``` -------------------------------- ### PHP: Await a Single Promise with await Source: https://github.com/nunomaduro/pokio/blob/main/README.md Provides an example of using the `await` function to block execution until a single promise resolves. This is the fundamental way to get the result of an asynchronous operation. ```PHP $promise = async(function () { sleep(2); return 1 + 1; }); var_dump(await($promise)); // int(2) ``` -------------------------------- ### Run Unit Tests Source: https://github.com/nunomaduro/pokio/blob/main/CONTRIBUTING.md Executes only the unit tests for the Pokio project. This is useful for quickly verifying the correctness of individual components. ```bash composer test:unit ``` -------------------------------- ### Run All Project Tests Source: https://github.com/nunomaduro/pokio/blob/main/CONTRIBUTING.md Executes all the tests defined for the Pokio project. This is essential to verify that new contributions do not break existing functionality. ```bash composer test ``` -------------------------------- ### Lint Code with Composer Source: https://github.com/nunomaduro/pokio/blob/main/CONTRIBUTING.md Runs the linting process for the project's code using Composer. This command helps ensure the code adheres to the project's coding style standards. ```bash composer lint ``` -------------------------------- ### Check Project Types Source: https://github.com/nunomaduro/pokio/blob/main/CONTRIBUTING.md Performs type checking on the project's code using Composer. This helps catch potential type-related errors before runtime. ```bash composer test:types ``` -------------------------------- ### PHP: Create and Await a Basic Promise with async Source: https://github.com/nunomaduro/pokio/blob/main/README.md Demonstrates the creation of a promise using the `async` function and awaiting its resolved value using the `await` function in PHP. The `async` function takes a closure that returns a value, and `await` blocks until the promise resolves. ```PHP $promise = async(function () { return 1 + 1; }); var_dump(await($promise)); // int(2) ``` -------------------------------- ### PHP: Handle Promise Exceptions with try-catch Source: https://github.com/nunomaduro/pokio/blob/main/README.md Demonstrates an alternative to the `catch` method for handling exceptions from promises using a native PHP `try-catch` block. This approach is useful when direct exception handling is preferred over promise chaining. ```PHP $promise = async(function () { throw new Exception('Error'); }); try { await($promise); } catch (Throwable $e) { var_dump('Rescued: ' . $e->getMessage()); // string(16) "Rescued: Error" } ``` -------------------------------- ### PHP: Chain then() methods on a Promise Source: https://github.com/nunomaduro/pokio/blob/main/README.md Shows how to chain multiple `then` methods to a promise created with `async`. Each `then` method accepts a closure that processes the resolved value of the previous step, allowing for sequential asynchronous operations. ```PHP $promise = async(fn (): int => 1 + 2) ->then(fn ($result): int => $result + 2) ->then(fn ($result): int => $result * 2); $result = await($promise); var_dump($result); // int(10) ``` -------------------------------- ### PHP: Await Multiple Promises Simultaneously Source: https://github.com/nunomaduro/pokio/blob/main/README.md Demonstrates how to use the `await` function with an array of promises to wait for all of them to resolve concurrently. The function returns an array containing the resolved values in the same order as the input promises. ```PHP $promiseA = async(function () { sleep(2); return 1 + 1; }); $promiseB = async(function () { sleep(2); return 2 + 2; }); var_dump(await([$promiseA, $promiseB])); // array(2) { [0]=> int(2) [1]=> int(4) } ``` -------------------------------- ### PHP: Handle Promise Rejection with catch() Source: https://github.com/nunomaduro/pokio/blob/main/README.md Illustrates how to use the `catch` method to handle exceptions thrown within an `async` promise. The `catch` method takes a closure that receives the `Throwable` object, allowing for error recovery or logging. ```PHP $promise = async(function () { throw new Exception('Error'); })->catch(function (Throwable $e) { return 'Rescued: ' . $e->getMessage(); }); var_dump(await($promise)); // string(16) "Rescued: Error" ``` -------------------------------- ### PHP: Await Nested Promises Source: https://github.com/nunomaduro/pokio/blob/main/README.md Explains how the `async` function automatically awaits promises returned from within its closure. This allows for the creation of nested asynchronous operations where the result of one promise is another promise. ```PHP $promise = async(function () { return async(function () { return 1 + 1; }); }); var_dump(await($promise)); // int(2) ``` -------------------------------- ### PHP: Execute finally() block on Promise completion Source: https://github.com/nunomaduro/pokio/blob/main/README.md Shows the usage of the `finally` method, which executes a given closure regardless of whether the promise resolves successfully or throws an exception. This is useful for cleanup operations. ```PHP $promise = async(function (): void { throw new RuntimeException('Exception 1'); })->finally(function () use (&$called): void { echo "Finally called\n"; }); ``` -------------------------------- ### PHP: Invoke Promise Directly for Resolved Value Source: https://github.com/nunomaduro/pokio/blob/main/README.md Shows an alternative to using the `await` function: invoking the promise object directly like a function. This also returns the resolved value of the promise, providing a more concise syntax in some cases. ```PHP $promise = async(fn (): int => 1 + 2); $result = $promise(); var_dump($result); // int(3) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.