From f3d73972361ca82f590b65b69834bfabbbe306d5 Mon Sep 17 00:00:00 2001 From: Shahzada Modassir <152081666+shahzadamodassir@users.noreply.github.com> Date: Sun, 10 Nov 2024 10:01:37 +0530 Subject: [PATCH] Update README.md --- README.md | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/README.md b/README.md index 413bbdd..5235fd5 100644 --- a/README.md +++ b/README.md @@ -1 +1,98 @@ # PHP Promise + +
+ +## Installation + +Installation is super-easy via [Composer](https://getcomposer.org) + +```bash +composer require modassir/promise +``` + +or add it by hand to your `composer.json` file. + +## Getting Started + +```php +(new Promise(function($resolve, $reject) { + $resolve('success'); + $reject('An error accurred.'); +}))->then(function($res) { + // echo : 'success' +})->catch(function() { + +})->finally(function() { + // echo: strtoupper('Hello Worlds!'); +}); + +(new Promise(function($resolve, $reject) { + $reject('An error accurred.'); + $resolve('success'); +}))->then(function() { + +})->catch(function($res) { + // echo: 'An error accurred.' +})->finally(function() { + // echo: strtoupper('Hello Worlds!'); +}); + +(new Promise(function($resolve, $reject) { + $resolve('success'); + $reject('An error accurred.'); + call_undfined_function(); +}))->then(function() { + +})->catch(function($res) { + $res->getMessage(); // Output: Call to undefined function call_undfined_function() + $res->getCode(); // Output: 0 + $res->getLine(); // Output: 8 + $res->getFile(); // Output: C:\xampp\htdocs\promise\index.php + $res->getTraceAsString(); // Output: #1 C:\xampp\htdocs\promise\index.php(9): Modassir\Promise\Promise->__construct(Object(Closure)) +})->finally(function() { + // echo: strtoupper('Hello Worlds!'); +}); +``` + +```php +$promise = new Promise(function($resolve, $reject) { + $resolve('success'); + $reject('An error accurred.'); +}); + +$promise->then(function($res) { + // echo : 'success' +})->catch(function() { + +})->finally(function() { + // echo: strtoupper('Hello Worlds!'); +}); + + +$promise = new Promise(function($resolve, $reject) { + $resolve('success'); + $reject('An error accurred.'); + call_undfined_function(); +}); + +$promise->then(function() { + +})->catch(function($res) { + $res->getMessage(); // Output: Call to undefined function call_undfined_function() + $res->getCode(); // Output: 0 + $res->getLine(); // Output: 8 + $res->getFile(); // Output: C:\xampp\htdocs\promise\index.php + $res->getTraceAsString(); // Output: #1 C:\xampp\htdocs\promise\index.php(9): Modassir\Promise\Promise->__construct(Object(Closure)) +})->finally(function() { + // echo: strtoupper('Hello Worlds!'); +}); +``` + +## Resources +- [Report issue](https://github.com/lazervel/promise/issues) and [send Pull Request](https://github.com/lazervel/promise/pulls) in the [main Lazervel repository](https://github.com/lazervel/promise)