# Guzzle Promises
[Promises/A+](https://promisesaplus.com/) implementation that handles promise
chaining and resolution iteratively, allowing for "infinite" promise chaining
while keeping the stack size constant. Read [this blog post](https://blog.domenic.me/youre-missing-the-point-of-promises/)
for a general introduction to promises.
- [Features](#features)
- [Quick start](#quick-start)
- [Synchronous wait](#synchronous-wait)
- [Cancellation](#cancellation)
- [API](#api)
- [Promise](#promise)
- [FulfilledPromise](#fulfilledpromise)
- [RejectedPromise](#rejectedpromise)
- [Promise interop](#promise-interop)
- [Implementation notes](#implementation-notes)
# Features
- [Promises/A+](https://promisesaplus.com/) implementation.
- Promise resolution and chaining is handled iteratively, allowing for
"infinite" promise chaining.
- Promises have a synchronous `wait` method.
- Promises can be cancelled.
- Works with any object that has a `then` function.
- C# style async/await coroutine promises using
`GuzzleHttp\Promise\coroutine()`.
# Quick start
A *promise* represents the eventual result of an asynchronous operation. The
primary way of interacting with a promise is through its `then` method, which
registers callbacks to receive either a promise's eventual value or the reason
why the promise cannot be fulfilled.
## Callbacks
Callbacks are registered with the `then` method by providing an optional
`$onFulfilled` followed by an optional `$onRejected` function.
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise();
$promise->then(
// $onFulfilled
function ($value) {
echo 'The promise was fulfilled.';
},
// $onRejected
function ($reason) {
echo 'The promise was rejected.';
}
);
```
*Resolving* a promise means that you either fulfill a promise with a *value* or
reject a promise with a *reason*. Resolving a promises triggers callbacks
registered with the promises's `then` method. These callbacks are triggered
only once and in the order in which they were added.
## Resolving a promise
Promises are fulfilled using the `resolve($value)` method. Resolving a promise
with any value other than a `GuzzleHttp\Promise\RejectedPromise` will trigger
all of the onFulfilled callbacks (resolving a promise with a rejected promise
will reject the promise and trigger the `$onRejected` callbacks).
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise();
$promise
->then(function ($value) {
// Return a value and don't break the chain
return "Hello, " . $value;
})
// This then is executed after the first then and receives the value
// returned from the first then.
->then(function ($value) {
echo $value;
});
// Resolving the promise triggers the $onFulfilled callbacks and outputs
// "Hello, reader".
$promise->resolve('reader.');
```
## Promise forwarding
Promises can be chained one after the other. Each then in the chain is a new
promise. The return value of of a promise is what's forwarded to the next
promise in the chain. Returning a promise in a `then` callback will cause the
subsequent promises in the chain to only be fulfilled when the returned promise
has been fulfilled. The next promise in the chain will be invoked with the
resolved value of the promise.
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise();
$nextPromise = new Promise();
$promise
->then(function ($value) use ($nextPromise) {
echo $value;
return $nextPromise;
})
->then(function ($value) {
echo $value;
});
// Triggers the first callback and outputs "A"
$promise->resolve('A');
// Triggers the second callback and outputs "B"
$nextPromise->resolve('B');
```
## Promise rejection
When a promise is rejected, the `$onRejected` callbacks are invoked with the
rejection reason.
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise();
$promise->then(null, function ($reason) {
echo $reason;
});
$promise->reject('Error!');
// Outputs "Error!"
```
## Rejection forwarding
If an exception is thrown in an `$onRejected` callback, subsequent
`$onRejected` callbacks are invoked with the thrown exception as the reason.
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise();
$promise->then(null, function ($reason) {
throw new \Exception($reason);
})->then(null, function ($reason) {
assert($reason->getMessage() === 'Error!');
});
$promise->reject('Error!');
```
You can also forward a rejection down the promise chain by returning a
`GuzzleHttp\Promise\RejectedPromise` in either an `$onFulfilled` or
`$onRejected` callback.
```php
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\RejectedPromise;
$promise = new Promise();
$promise->then(null, function ($reason) {
return new RejectedPromise($reason);
})->then(null, function ($reason) {
assert($reason === 'Error!');
});
$promise->reject('Error!');
```
If an exception is not thrown in a `$onRejected` callback and the callback
does not return a rejected promise, downstream `$onFulfilled` callbacks are
invoked using the value returned from the `$onRejected` callback.
```php
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\RejectedPromise;
$promise = new Promise();
$promise
->then(null, function ($reason) {
return "It's ok";
})
->then(function ($value) {
assert($value === "It's ok");
});
$promise->reject('Error!');
```
# Synchronous wait
You can synchronously force promises to complete using a promise's `wait`
method. When creating a promise, you can provide a wait function that is used
to synchronously force a promise to complete. When a wait function is invoked
it is expected to deliver a value to the promise or reject the promise. If the
wait function does not deliver a value, then an exception is thrown. The wait
function provided to a promise constructor is invoked when the `wait` function
of the promise is called.
```php
$promise = new Promise(function () use (&$promise) {
$promise->resolve('foo');
});
// Calling wait will return the value of the promise.
echo $promise->wait(); // outputs "foo"
```
If an exception is encountered while invoking the wait function of a promise,
the promise is rejected with the exception and the exception is thrown.
```php
$promise = new Promise(function () use (&$promise) {
throw new \Exception('foo');
});
$promise->wait(); // throws the exception.
```
Calling `wait` on a promise that has been fulfilled will not trigger the wait
function. It will simply return the previously resolved value.
```php
$promise = new Promise(function () { die('this is not called!'); });
$promise->resolve('foo');
echo $promise->wait(); // outputs "foo"
```
Calling `wait` on a promise that has been rejected will throw an exception. If
the rejection reason is an instance of `\Exception` the reason is thrown.
Otherwise, a `GuzzleHttp\Promise\RejectionException` is thrown and the reason
can be obtained by calling the `getReason` method of the exception.
```php
$promise = new Promise();
$promise->reject('foo');
$promise->wait();
```
> PHP Fatal error: Uncaught exception 'GuzzleHttp\Promise\RejectionException' with message 'The promise was rejected with value: foo'
## Unwrapping a promise
When synchronously waiting on a promise, you are joining the state of the
promise into the current state of execution (i.e., return the value of the
promise if it was fulfilled or throw an exception if it was rejected). This is
called "unwrapping" the promise. Waiting on a promise will by default unwrap
the promise state.
You can force a promise to resolve and *not* unwrap the state of the promise
by passing `false` to the first argument of the `wait` function:
```php
$promise = new Promise();
$promise->reject('foo');
// This will not throw an exception. It simply ensures the promise has
// been resolved.
$promise->wait(false);
```
When unwrapping a promise, the resolved value of the prom
没有合适的资源?快使用搜索试试~ 我知道了~
PHP实例开发源码—Oxwall (SNS社交网络系统).zip
共2000个文件
php:2037个
png:1690个
html:571个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 38 浏览量
2022-11-22
02:46:11
上传
评论
收藏 17.27MB ZIP 举报
温馨提示
PHP实例开发源码—Oxwall (SNS社交网络系统).zip
资源推荐
资源详情
资源评论
收起资源包目录
PHP实例开发源码—Oxwall (SNS社交网络系统).zip (2000个子文件)
.blank 0B
test.conf 65B
fb_ca_chain_bundle.crt 247KB
base.css 210KB
base.css 135KB
base.css 135KB
base.css 133KB
base.css 133KB
base.css 133KB
base.css 133KB
base.css 129KB
ow.css 74KB
admin.css 31KB
mailbox.css 19KB
browse_photo.css 14KB
photo_upload.css 13KB
browse_files.css 11KB
photo_floatbox.css 10KB
photo_floatbox.css 9KB
shCoreEclipse.css 8KB
file_upload.css 8KB
shCoreDjango.css 7KB
shCoreFadeToGrey.css 7KB
shCoreDefault.css 7KB
shCoreMidnight.css 7KB
shCoreRDark.css 7KB
shCoreEmacs.css 7KB
shCoreMDUltra.css 7KB
shCore.css 5KB
edit_photo.css 4KB
suitup.css 4KB
style.css 4KB
shThemeEclipse.css 3KB
shThemeDjango.css 2KB
shThemeFadeToGrey.css 2KB
shThemeVisualStudio.css 2KB
shThemeDefault.css 2KB
shThemeMidnight.css 2KB
shThemeRDark.css 2KB
shThemeEmacs.css 2KB
shThemeMDUltra.css 2KB
jquery.Jcrop.min.css 2KB
htmlarea.css 2KB
shThemeAppleScript.css 2KB
admin.css 1KB
style.css 875B
fbconnect.css 830B
jquery.Jcrop.css 755B
style.css 689B
prettify.css 689B
mobile.css 198B
config.php.default 842B
travis.phpunit.xml.dist 1KB
phpunit.xml.dist 411B
phpunit.xml.dist 411B
simplicity_light-webfont.eot 88KB
simplicity_light-webfont.eot 88KB
Ubuntu-B-webfont.eot 54KB
Ubuntu-B-webfont.eot 54KB
Ubuntu-R-webfont.eot 32KB
Ubuntu-R-webfont.eot 32KB
simplicity-webfont.eot 29KB
simplicity-webfont.eot 29KB
simplicity_medium-webfont.eot 29KB
helveticaneuecyr-medium-webfont.eot 21KB
helveticaneuecyr-medium-webfont.eot 21KB
helveticaneuecyr-medium-webfont.eot 21KB
helveticaneuecyr-medium-webfont.eot 21KB
helveticaneuecyr-bold-webfont.eot 20KB
helveticaneuecyr-bold-webfont.eot 20KB
helveticaneuecyr-bold-webfont.eot 20KB
helveticaneuecyr-bold-webfont.eot 20KB
opensans-condbold-webfont.eot 20KB
floatbox_preloader.gif 10KB
loadingAnimation.gif 6KB
ajax_preloader_content.gif 5KB
ajax_preloader_button.gif 5KB
preloader_circle@2x.gif 4KB
preloader_circle@2x.gif 4KB
preloader_circle@2x.gif 4KB
preloader_circle@2x.gif 4KB
preloader_circle.gif 2KB
preloader_circle.gif 2KB
preloader_circle.gif 2KB
preloader_circle.gif 2KB
avatar-bg.gif 2KB
preloader-big.gif 2KB
inputbg.gif 1KB
btn_main_bg.gif 1KB
btn_main_bg.gif 1KB
btn_main_bg_hover.gif 1KB
box_menu_active.gif 1KB
console_bg_hover.gif 1KB
box_menu.gif 1KB
box_menu_active.gif 1KB
console_active_item_bg.gif 1KB
context_action.gif 1KB
console_item_bg.gif 1KB
box_cap.gif 1KB
btn_bg.gif 1KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
易小侠
- 粉丝: 6453
- 资源: 9万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功