CINXE.COM
react/promise - Packagist
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>react/promise - Packagist</title> <meta name="description" content="A lightweight implementation of CommonJS Promises/A for PHP" /> <meta name="author" content="Jordi Boggiano" /> <link rel="canonical" href="https://packagist.org/packages/react/promise" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="shortcut icon" href="/favicon.ico?v=1739794478" /> <link rel="stylesheet" href="/build/app.css?v=1739794478" /> <link rel="alternate" type="application/rss+xml" title="New Releases - react/promise" href="https://packagist.org/feeds/package.react/promise.rss" /> <link rel="alternate" type="application/rss+xml" title="New Releases - react" href="https://packagist.org/feeds/vendor.react.rss" /> <link rel="alternate" type="application/rss+xml" title="Newly Submitted Packages - Packagist" href="https://packagist.org/feeds/packages.rss" /> <link rel="alternate" type="application/rss+xml" title="New Releases - Packagist" href="https://packagist.org/feeds/releases.rss" /> <link rel="search" type="application/opensearchdescription+xml" href="/search.osd?v=1739794478" title="Packagist" /> <script nonce="VL78G2lBRZDnitlw9l5HJw=="> var onloadRecaptchaCallback = function() { document.querySelectorAll('.recaptcha-protected').forEach((el) => { el.closest('form').querySelectorAll('button, input[type="submit"]').forEach((submit) => { grecaptcha.render(submit, { 'sitekey' : '6LfKiPoUAAAAAGlKWkhEEx7X8muIVYy6oA0GNdX0', 'callback' : function (token) { submit.form.submit(); } }); }) }); }; </script> <link rel="stylesheet" href="/css/github/markdown.css?v=1739794478"> </head> <body> <section class="wrap"> <header class="navbar-wrapper navbar-fixed-top"> <section class="banner alert-error hidden"> Composer v1 support is <a href="https://blog.packagist.com/shutting-down-packagist-org-support-for-composer-1-x/">coming to an end</a> <i class="glyphicon glyphicon-remove banner-close" data-banner-id="composer1eol"></i> </section> <nav class="container"> <div class="navbar" role="navigation"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <h1 class="navbar-brand"><a href="/">Packagist</a> <em class="hidden-sm hidden-xs">The PHP Package Repository</em></h1> </div> <div class="collapse navbar-collapse"> <ul class="nav navbar-nav"> <li> <a href="/explore/">Browse</a> </li> <li> <a href="/packages/submit">Submit</a> </li> <li> <a href="/register/">Create account</a> </li> <li class="nav-user"> <section class="nav-user-signin"> <a href="/login/">Sign in</a> <section class="signin-box"> <form action="/login/" method="POST" id="nav_login"> <div class="input-group"> <input class="form-control" type="text" id="_username" name="_username" placeholder="Username / Email"> <span class="input-group-addon"><span class="icon-user"></span></span> </div> <div class="input-group"> <input class="form-control" type="password" id="_password" name="_password" placeholder="Password"> <span class="input-group-addon"><span class="icon-lock"></span></span> </div> <div class="checkbox"> <label for="_remember_me"> <input type="checkbox" id="_remember_me" name="_remember_me" value="on" checked="checked" /> Remember me </label> </div> <div class="signin-box-buttons"> <a href="/login/github" class="pull-right btn btn-primary btn-github"><span class="icon-github"></span>Use Github</a> <button type="submit" class="btn btn-success" id="_submit_mini" name="_submit">Log in</button> </div> </form> <div class="signin-box-register"> <a href="/register/">No account yet? Create one now!</a> </div> </section> </section> </li> </ul> </div> </div> </nav> </header> <section class="wrapper wrapper-search"> <div class="container "> <form name="search_query" method="get" action="/search/" id="search-form" autocomplete="off"> <div class="sortable row"> <div class="col-xs-12 js-search-field-wrapper col-md-9"> <input type="search" id="search_query_query" name="query" required="required" autocomplete="off" placeholder="Search packages..." tabindex="1" class=" form-control" value="" /> </div> </div> </form> </div> </section> <section class="wrapper"> <section class="container content" role="main"> <div id="search-container" class="hidden"> <div class="row"> <div class="search-list col-md-9"></div> <div class="search-facets col-md-3"> <div class="search-facets-active-filters"></div> <div class="search-facets-type"></div> <div class="search-facets-tags"></div> </div> </div> <div class="row"> <div class="col-md-9"> <div id="powered-by"> Search by <a href="https://www.algolia.com/"><img src="/img/algolia-logo-light.svg?v=1739794478"></a> </div> <div id="pagination-container"> <div class="pagination"></div> </div> </div> </div> </div> <div class="row" id="view-package-page"> <div class="col-xs-12 package"> <div class="package-header"> <div class="row"> <div class="col-md-9"> <h2 class="title"> <a href="/packages/react/">react<span class="hidden"> </span>/</a><span class="hidden"> </span>promise </h2> </div> </div> </div> <div class="row"> <div class="col-md-8"> <p class="requireme"><i class="glyphicon glyphicon-save"></i> <input type="text" readonly="readonly" value="composer require react/promise" /></p> <p class="description">A lightweight implementation of CommonJS Promises/A for PHP</p> </div> <div class="col-md-offset-1 col-md-3"> <div class="row package-aside"> <div class="details col-xs-12 col-sm-6 col-md-12"> <h5>Maintainers</h5> <p class="maintainers"> <a href="/users/jsor/"><img width="48" height="48" title="jsor" src="https://www.gravatar.com/avatar/eacba885ec98c1cfea23905e2869714e?s=48&d=identicon" srcset="https://www.gravatar.com/avatar/eacba885ec98c1cfea23905e2869714e?s=96&d=identicon 2x"></a> <a href="/users/igorw/"><img width="48" height="48" title="igorw" src="https://www.gravatar.com/avatar/eb42ff31986f5ed1d97580819134d702?s=48&d=identicon" srcset="https://www.gravatar.com/avatar/eb42ff31986f5ed1d97580819134d702?s=96&d=identicon 2x"></a> <a href="/users/cboden/"><img width="48" height="48" title="cboden" src="https://www.gravatar.com/avatar/e64cdd88cc31a6f8423237f38a0c97eb?s=48&d=identicon" srcset="https://www.gravatar.com/avatar/e64cdd88cc31a6f8423237f38a0c97eb?s=96&d=identicon 2x"></a> <a href="/users/WyriHaximus/"><img width="48" height="48" title="WyriHaximus" src="https://www.gravatar.com/avatar/ee7772711b5657411beca330d84b8982?s=48&d=identicon" srcset="https://www.gravatar.com/avatar/ee7772711b5657411beca330d84b8982?s=96&d=identicon 2x"></a> <a href="/users/clue/"><img width="48" height="48" title="clue" src="https://www.gravatar.com/avatar/d1b6700884ac0ae368918ad171bb6a75?s=48&d=identicon" srcset="https://www.gravatar.com/avatar/d1b6700884ac0ae368918ad171bb6a75?s=96&d=identicon 2x"></a> </p> <h5>Details</h5> <p class="canonical"> <a href="https://github.com/reactphp/promise" title="Canonical Repository URL">github.com/reactphp/promise</a> </p> <p><a rel="nofollow noopener external noindex ugc" href="https://github.com/reactphp/promise/tree/3.x">Source</a></p> <p><a rel="nofollow noopener external noindex ugc" href="https://github.com/reactphp/promise/issues">Issues</a></p> </div> <div class="facts funding col-xs-12 col-sm-6 col-md-12"> <p> Fund package maintenance! <br><a rel="nofollow noopener external noindex ugc" href="https://opencollective.com/reactphp"> <i class="glyphicon glyphicon-link"></i>Open Collective </a> </div> <div class="facts col-xs-12 col-sm-6 col-md-12"> <p> <span> <a href="/packages/react/promise/stats" rel="nofollow">Installs</a>: </span> 272 228 000 </p> <p> <span> <a href="/packages/react/promise/dependents?order_by=downloads" rel="nofollow">Dependents</a>: </span> 633 </p> <p> <span> <a href="/packages/react/promise/suggesters" rel="nofollow">Suggesters</a>: </span> 21 </p> <p> <span> <a href="/packages/react/promise/advisories" rel="nofollow">Security</a>: </span> 0 </p> <p> <span> <a href="https://github.com/reactphp/promise/stargazers">Stars</a>: </span> 2 409 </p> <p> <span> <a href="https://github.com/reactphp/promise/watchers">Watchers</a>: </span> 50 </p> <p> <span> <a href="https://github.com/reactphp/promise/forks">Forks</a>: </span> 147 </p> <p> <span> <a rel="nofollow noopener external noindex ugc" href="https://github.com/reactphp/promise/issues">Open Issues</a>: </span> 2 </p> </div> </div> </div> </div> <div class="row versions-section"> <div class="version-details col-md-9"> <div class="title"> <span class="version-number">v3.2.0</span> <span class="release-date">2024-05-24 10:39 UTC</span> </div> <div class="clearfix package-links"> <div class="row"> <div class="clearfix visible-sm-block"></div> <div class="clearfix visible-md-block visible-lg-block"></div> <div class="requires col-sm-6 col-md-4 "> <p class="link-type">Requires</p> <ul><li>php: >=7.1.0</li></ul> </div> <div class="devRequires col-sm-6 col-md-4 "> <p class="link-type">Requires (Dev)</p> <ul><li><a href="/packages/phpstan/phpstan">phpstan/phpstan</a>: 1.10.39 || 1.4.10</li><li><a href="/packages/phpunit/phpunit">phpunit/phpunit</a>: ^9.6 || ^7.5</li></ul> </div> <div class="clearfix visible-sm-block"></div> <div class="suggests col-sm-6 col-md-4 hidden-xs"> <p class="link-type">Suggests</p> <p class="no-links">None</p> </div> <div class="clearfix visible-md-block visible-lg-block"></div> <div class="provides col-sm-6 col-md-4 hidden-xs"> <p class="link-type">Provides</p> <p class="no-links">None</p> </div> <div class="clearfix visible-sm-block"></div> <div class="conflicts col-sm-6 col-md-4 hidden-xs"> <p class="link-type">Conflicts</p> <p class="no-links">None</p> </div> <div class="replaces col-sm-6 col-md-4 hidden-xs"> <p class="link-type">Replaces</p> <p class="no-links">None</p> </div> </div> </div> <div class="metadata"> <p class="license"><i class="glyphicon glyphicon-copyright-mark" title="License"></i> MIT <span class="source-reference"><i class="glyphicon glyphicon-bookmark" title="Source Reference"></i> 8a164643313c71354582dc850b42b33fa12a4b63</span></p> <i class="glyphicon glyphicon-user" title="Authors"></i> <ul class="authors"> <li><a rel="nofollow noopener external noindex ugc" href="https://sorgalla.com/">Jan Sorgalla</a> <span class="visible-sm-inline visible-md-inline visible-lg-inline"><jsorgalla<span style="display:none">.woop</span>@gmail.com></span></li> <li><a rel="nofollow noopener external noindex ugc" href="https://clue.engineering/">Christian L眉ck</a> <span class="visible-sm-inline visible-md-inline visible-lg-inline"><christian<span style="display:none">.woop</span>@clue.engineering></span></li> <li><a rel="nofollow noopener external noindex ugc" href="https://wyrihaximus.net/">Cees-Jan Kiewiet</a> <span class="visible-sm-inline visible-md-inline visible-lg-inline"><reactphp<span style="display:none">.woop</span>@ceesjankiewiet.nl></span></li> <li><a rel="nofollow noopener external noindex ugc" href="https://cboden.dev/">Chris Boden</a> <span class="visible-sm-inline visible-md-inline visible-lg-inline"><cboden<span style="display:none">.woop</span>@gmail.com></span></li> </ul> <p class="tags"> <i class="glyphicon glyphicon-tag" title="Tags"></i> <a href="https://packagist.org/search/?tags=promises">promises</a><a href="https://packagist.org/search/?tags=promise">promise</a></p> </div> </div> <div class="col-md-3 no-padding"> <div class="package-aside versions-wrapper"> <ul class="versions"> <li class="details-toggler version" data-version-id="3.x-dev" data-load-more="/versions/5959291.json"> <a href="#3.x-dev" class="version-number">3.x-dev</a> </li> <li class="details-toggler version open" data-version-id="v3.2.0" data-load-more="/versions/8184947.json"> <a href="#v3.2.0" class="version-number">v3.2.0</a> </li> <li class="details-toggler version" data-version-id="v3.1.0" data-load-more="/versions/7687573.json"> <a href="#v3.1.0" class="version-number">v3.1.0</a> </li> <li class="details-toggler version" data-version-id="v3.0.0" data-load-more="/versions/7349410.json"> <a href="#v3.0.0" class="version-number">v3.0.0</a> </li> <li class="details-toggler version" data-version-id="2.x-dev" data-load-more="/versions/911799.json"> <a href="#2.x-dev" class="version-number">2.x-dev</a> </li> <li class="details-toggler version" data-version-id="v2.11.0" data-load-more="/versions/7687562.json"> <a href="#v2.11.0" class="version-number">v2.11.0</a> </li> <li class="details-toggler version" data-version-id="v2.10.0" data-load-more="/versions/7169250.json"> <a href="#v2.10.0" class="version-number">v2.10.0</a> </li> <li class="details-toggler version" data-version-id="v2.9.0" data-load-more="/versions/5958077.json"> <a href="#v2.9.0" class="version-number">v2.9.0</a> </li> <li class="details-toggler version" data-version-id="v2.8.0" data-load-more="/versions/3859686.json"> <a href="#v2.8.0" class="version-number">v2.8.0</a> </li> <li class="details-toggler version" data-version-id="v2.7.1" data-load-more="/versions/2675488.json"> <a href="#v2.7.1" class="version-number">v2.7.1</a> </li> <li class="details-toggler version" data-version-id="v2.7.0" data-load-more="/versions/2260168.json"> <a href="#v2.7.0" class="version-number">v2.7.0</a> </li> <li class="details-toggler version" data-version-id="v2.6.0" data-load-more="/versions/2254200.json"> <a href="#v2.6.0" class="version-number">v2.6.0</a> </li> <li class="details-toggler version" data-version-id="v2.5.1" data-load-more="/versions/1307187.json"> <a href="#v2.5.1" class="version-number">v2.5.1</a> </li> <li class="details-toggler version" data-version-id="v2.5.0" data-load-more="/versions/1179613.json"> <a href="#v2.5.0" class="version-number">v2.5.0</a> </li> <li class="details-toggler version" data-version-id="v2.4.1" data-load-more="/versions/1179612.json"> <a href="#v2.4.1" class="version-number">v2.4.1</a> </li> <li class="details-toggler version" data-version-id="v2.4.0" data-load-more="/versions/1179611.json"> <a href="#v2.4.0" class="version-number">v2.4.0</a> </li> <li class="details-toggler version" data-version-id="v2.3.0" data-load-more="/versions/1179610.json"> <a href="#v2.3.0" class="version-number">v2.3.0</a> </li> <li class="details-toggler version" data-version-id="v2.2.2" data-load-more="/versions/1179609.json"> <a href="#v2.2.2" class="version-number">v2.2.2</a> </li> <li class="details-toggler version" data-version-id="v2.2.1" data-load-more="/versions/1179608.json"> <a href="#v2.2.1" class="version-number">v2.2.1</a> </li> <li class="details-toggler version" data-version-id="v2.2.0" data-load-more="/versions/1179607.json"> <a href="#v2.2.0" class="version-number">v2.2.0</a> </li> <li class="details-toggler version" data-version-id="v2.1.0" data-load-more="/versions/1179606.json"> <a href="#v2.1.0" class="version-number">v2.1.0</a> </li> <li class="details-toggler version" data-version-id="v2.0.0" data-load-more="/versions/1179605.json"> <a href="#v2.0.0" class="version-number">v2.0.0</a> </li> <li class="details-toggler version" data-version-id="1.x-dev" data-load-more="/versions/911798.json"> <a href="#1.x-dev" class="version-number">1.x-dev</a> </li> <li class="details-toggler version" data-version-id="v1.3.0" data-load-more="/versions/7687549.json"> <a href="#v1.3.0" class="version-number">v1.3.0</a> </li> <li class="details-toggler version" data-version-id="v1.2.1" data-load-more="/versions/1179604.json"> <a href="#v1.2.1" class="version-number">v1.2.1</a> </li> <li class="details-toggler version" data-version-id="v1.2.0" data-load-more="/versions/1179603.json"> <a href="#v1.2.0" class="version-number">v1.2.0</a> </li> <li class="details-toggler version" data-version-id="v1.1.0" data-load-more="/versions/1179602.json"> <a href="#v1.1.0" class="version-number">v1.1.0</a> </li> <li class="details-toggler version" data-version-id="v1.0.4" data-load-more="/versions/1179601.json"> <a href="#v1.0.4" class="version-number">v1.0.4</a> </li> <li class="details-toggler version" data-version-id="v1.0.3" data-load-more="/versions/1179600.json"> <a href="#v1.0.3" class="version-number">v1.0.3</a> </li> <li class="details-toggler version" data-version-id="v1.0.2" data-load-more="/versions/1179599.json"> <a href="#v1.0.2" class="version-number">v1.0.2</a> </li> <li class="details-toggler version" data-version-id="v1.0.1" data-load-more="/versions/1179598.json"> <a href="#v1.0.1" class="version-number">v1.0.1</a> </li> <li class="details-toggler version last" data-version-id="v1.0.0" data-load-more="/versions/1179597.json"> <a href="#v1.0.0" class="version-number">v1.0.0</a> </li> </ul> <div class="hidden versions-expander"> <i class="glyphicon glyphicon-chevron-down"></i> </div> </div> <div class="last-update"> <p class="auto-update-success">This package is auto-updated.</p> <p>Last update: 2025-01-19 18:52:34 UTC </p> </div> </div> </div> <hr class="clearfix"> <div class="readme markdown-body"> <h1>README</h1> <a id="user-content-promise" class="anchor" href="#user-content-promise" rel="nofollow noindex noopener external ugc"></a> <p>A lightweight implementation of <a href="http://wiki.commonjs.org/wiki/Promises/A" rel="nofollow noindex noopener external ugc">CommonJS Promises/A</a> for PHP.</p> <p><a href="https://github.com/reactphp/promise/actions" rel="nofollow noindex noopener external ugc"><img src="https://github.com/reactphp/promise/workflows/CI/badge.svg" alt="CI status" /></a> <a href="https://packagist.org/packages/react/promise" rel="nofollow noindex noopener external ugc"><img src="https://camo.githubusercontent.com/6c4da0732636c917b677a46214c069844b215d952971bb1a98818581e5d31d9e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72656163742f70726f6d6973653f636f6c6f723d626c7565266c6162656c3d696e7374616c6c732532306f6e2532305061636b6167697374" alt="installs on Packagist" /></a></p> <h2 class="heading-element">Table of Contents</h2><a id="user-content-table-of-contents" class="anchor" href="#user-content-table-of-contents" rel="nofollow noindex noopener external ugc"></a> <ol> <li><a href="#user-content-introduction" rel="nofollow noindex noopener external ugc">Introduction</a></li> <li><a href="#user-content-concepts" rel="nofollow noindex noopener external ugc">Concepts</a> <ul> <li><a href="#user-content-deferred" rel="nofollow noindex noopener external ugc">Deferred</a></li> <li><a href="#user-content-promise-1" rel="nofollow noindex noopener external ugc">Promise</a></li> </ul> </li> <li><a href="#user-content-api" rel="nofollow noindex noopener external ugc">API</a> <ul> <li><a href="#user-content-deferred-1" rel="nofollow noindex noopener external ugc">Deferred</a> <ul> <li><a href="#user-content-deferredpromise" rel="nofollow noindex noopener external ugc">Deferred::promise()</a></li> <li><a href="#user-content-deferredresolve" rel="nofollow noindex noopener external ugc">Deferred::resolve()</a></li> <li><a href="#user-content-deferredreject" rel="nofollow noindex noopener external ugc">Deferred::reject()</a></li> </ul> </li> <li><a href="#user-content-promiseinterface" rel="nofollow noindex noopener external ugc">PromiseInterface</a> <ul> <li><a href="#user-content-promiseinterfacethen" rel="nofollow noindex noopener external ugc">PromiseInterface::then()</a></li> <li><a href="#user-content-promiseinterfacecatch" rel="nofollow noindex noopener external ugc">PromiseInterface::catch()</a></li> <li><a href="#user-content-promiseinterfacefinally" rel="nofollow noindex noopener external ugc">PromiseInterface::finally()</a></li> <li><a href="#user-content-promiseinterfacecancel" rel="nofollow noindex noopener external ugc">PromiseInterface::cancel()</a></li> <li><a href="#user-content-promiseinterfaceotherwise" rel="nofollow noindex noopener external ugc"><del>PromiseInterface::otherwise()</del></a></li> <li><a href="#user-content-promiseinterfacealways" rel="nofollow noindex noopener external ugc"><del>PromiseInterface::always()</del></a></li> </ul> </li> <li><a href="#user-content-promise-2" rel="nofollow noindex noopener external ugc">Promise</a></li> <li><a href="#user-content-functions" rel="nofollow noindex noopener external ugc">Functions</a> <ul> <li><a href="#user-content-resolve" rel="nofollow noindex noopener external ugc">resolve()</a></li> <li><a href="#user-content-reject" rel="nofollow noindex noopener external ugc">reject()</a></li> <li><a href="#user-content-all" rel="nofollow noindex noopener external ugc">all()</a></li> <li><a href="#user-content-race" rel="nofollow noindex noopener external ugc">race()</a></li> <li><a href="#user-content-any" rel="nofollow noindex noopener external ugc">any()</a></li> <li><a href="#user-content-set_rejection_handler" rel="nofollow noindex noopener external ugc">set_rejection_handler()</a></li> </ul> </li> </ul> </li> <li><a href="#user-content-examples" rel="nofollow noindex noopener external ugc">Examples</a> <ul> <li><a href="#user-content-how-to-use-deferred" rel="nofollow noindex noopener external ugc">How to use Deferred</a></li> <li><a href="#user-content-how-promise-forwarding-works" rel="nofollow noindex noopener external ugc">How promise forwarding works</a> <ul> <li><a href="#user-content-resolution-forwarding" rel="nofollow noindex noopener external ugc">Resolution forwarding</a></li> <li><a href="#user-content-rejection-forwarding" rel="nofollow noindex noopener external ugc">Rejection forwarding</a></li> <li><a href="#user-content-mixed-resolution-and-rejection-forwarding" rel="nofollow noindex noopener external ugc">Mixed resolution and rejection forwarding</a></li> </ul> </li> </ul> </li> <li><a href="#user-content-install" rel="nofollow noindex noopener external ugc">Install</a></li> <li><a href="#user-content-tests" rel="nofollow noindex noopener external ugc">Tests</a></li> <li><a href="#user-content-credits" rel="nofollow noindex noopener external ugc">Credits</a></li> <li><a href="#user-content-license" rel="nofollow noindex noopener external ugc">License</a></li> </ol> <h2 class="heading-element">Introduction</h2><a id="user-content-introduction" class="anchor" href="#user-content-introduction" rel="nofollow noindex noopener external ugc"></a> <p>Promise is a library implementing <a href="http://wiki.commonjs.org/wiki/Promises/A" rel="nofollow noindex noopener external ugc">CommonJS Promises/A</a> for PHP.</p> <p>It also provides several other useful promise-related concepts, such as joining multiple promises and mapping and reducing collections of promises.</p> <p>If you've never heard about promises before, <a href="https://gist.github.com/domenic/3889970" rel="nofollow noindex noopener external ugc">read this first</a>.</p> <h2 class="heading-element">Concepts</h2><a id="user-content-concepts" class="anchor" href="#user-content-concepts" rel="nofollow noindex noopener external ugc"></a> <h3 class="heading-element">Deferred</h3><a id="user-content-deferred" class="anchor" href="#user-content-deferred" rel="nofollow noindex noopener external ugc"></a> <p>A <strong>Deferred</strong> represents a computation or unit of work that may not have completed yet. Typically (but not always), that computation will be something that executes asynchronously and completes at some point in the future.</p> <h3 class="heading-element">Promise</h3><a id="user-content-promise-1" class="anchor" href="#user-content-promise-1" rel="nofollow noindex noopener external ugc"></a> <p>While a deferred represents the computation itself, a <strong>Promise</strong> represents the result of that computation. Thus, each deferred has a promise that acts as a placeholder for its actual result.</p> <h2 class="heading-element">API</h2><a id="user-content-api" class="anchor" href="#user-content-api" rel="nofollow noindex noopener external ugc"></a> <h3 class="heading-element">Deferred</h3><a id="user-content-deferred-1" class="anchor" href="#user-content-deferred-1" rel="nofollow noindex noopener external ugc"></a> <p>A deferred represents an operation whose resolution is pending. It has separate promise and resolver parts.</p> <pre><span class="pl-s1"><span class="pl-c1">$</span>deferred</span> = <span class="pl-k">new</span> <span class="pl-v">React</span>\<span class="pl-v">Promise</span>\<span class="pl-v">Deferred</span>(); <span class="pl-s1"><span class="pl-c1">$</span>promise</span> = <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">promise</span>(); <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">resolve</span>(mixed <span class="pl-s1"><span class="pl-c1">$</span>value</span>); <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">reject</span>(\Throwable <span class="pl-s1"><span class="pl-c1">$</span>reason</span>);</pre> <p>The <code>promise</code> method returns the promise of the deferred.</p> <p>The <code>resolve</code> and <code>reject</code> methods control the state of the deferred.</p> <p>The constructor of the <code>Deferred</code> accepts an optional <code>$canceller</code> argument. See <a href="#user-content-promise-2" rel="nofollow noindex noopener external ugc">Promise</a> for more information.</p> <h4 class="heading-element">Deferred::promise()</h4><a id="user-content-deferredpromise" class="anchor" href="#user-content-deferredpromise" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span> = <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">promise</span>();</pre> <p>Returns the promise of the deferred, which you can hand out to others while keeping the authority to modify its state to yourself.</p> <h4 class="heading-element">Deferred::resolve()</h4><a id="user-content-deferredresolve" class="anchor" href="#user-content-deferredresolve" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">resolve</span>(mixed <span class="pl-s1"><span class="pl-c1">$</span>value</span>);</pre> <p>Resolves the promise returned by <code>promise()</code>. All consumers are notified by having <code>$onFulfilled</code> (which they registered via <code>$promise->then()</code>) called with <code>$value</code>.</p> <p>If <code>$value</code> itself is a promise, the promise will transition to the state of this promise once it is resolved.</p> <p>See also the <a href="#user-content-resolve" rel="nofollow noindex noopener external ugc"><code>resolve()</code> function</a>.</p> <h4 class="heading-element">Deferred::reject()</h4><a id="user-content-deferredreject" class="anchor" href="#user-content-deferredreject" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">reject</span>(\Throwable <span class="pl-s1"><span class="pl-c1">$</span>reason</span>);</pre> <p>Rejects the promise returned by <code>promise()</code>, signalling that the deferred's computation failed. All consumers are notified by having <code>$onRejected</code> (which they registered via <code>$promise->then()</code>) called with <code>$reason</code>.</p> <p>See also the <a href="#user-content-reject" rel="nofollow noindex noopener external ugc"><code>reject()</code> function</a>.</p> <h3 class="heading-element">PromiseInterface</h3><a id="user-content-promiseinterface" class="anchor" href="#user-content-promiseinterface" rel="nofollow noindex noopener external ugc"></a> <p>The promise interface provides the common interface for all promise implementations. See <a href="#user-content-promise-2" rel="nofollow noindex noopener external ugc">Promise</a> for the only public implementation exposed by this package.</p> <p>A promise represents an eventual outcome, which is either fulfillment (success) and an associated value, or rejection (failure) and an associated reason.</p> <p>Once in the fulfilled or rejected state, a promise becomes immutable. Neither its state nor its result (or error) can be modified.</p> <h4 class="heading-element">PromiseInterface::then()</h4><a id="user-content-promiseinterfacethen" class="anchor" href="#user-content-promiseinterfacethen" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>transformedPromise</span> = <span class="pl-s1"><span class="pl-c1">$</span>promise</span>-><span class="pl-en">then</span>(callable <span class="pl-s1"><span class="pl-c1">$</span>onFulfilled</span> = <span class="pl-c1">null</span>, callable <span class="pl-s1"><span class="pl-c1">$</span>onRejected</span> = <span class="pl-c1">null</span>);</pre> <p>Transforms a promise's value by applying a function to the promise's fulfillment or rejection value. Returns a new promise for the transformed result.</p> <p>The <code>then()</code> method registers new fulfilled and rejection handlers with a promise (all parameters are optional):</p> <ul> <li><code>$onFulfilled</code> will be invoked once the promise is fulfilled and passed the result as the first argument.</li> <li><code>$onRejected</code> will be invoked once the promise is rejected and passed the reason as the first argument.</li> </ul> <p>It returns a new promise that will fulfill with the return value of either <code>$onFulfilled</code> or <code>$onRejected</code>, whichever is called, or will reject with the thrown exception if either throws.</p> <p>A promise makes the following guarantees about handlers registered in the same call to <code>then()</code>:</p> <ol> <li>Only one of <code>$onFulfilled</code> or <code>$onRejected</code> will be called, never both.</li> <li><code>$onFulfilled</code> and <code>$onRejected</code> will never be called more than once.</li> </ol> <h4 class="heading-element">See also</h4><a id="user-content-see-also" class="anchor" href="#user-content-see-also" rel="nofollow noindex noopener external ugc"></a> <ul> <li><a href="#user-content-resolve" rel="nofollow noindex noopener external ugc">resolve()</a> - Creating a resolved promise</li> <li><a href="#user-content-reject" rel="nofollow noindex noopener external ugc">reject()</a> - Creating a rejected promise</li> </ul> <h4 class="heading-element">PromiseInterface::catch()</h4><a id="user-content-promiseinterfacecatch" class="anchor" href="#user-content-promiseinterfacecatch" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span>-><span class="pl-en">catch</span>(callable <span class="pl-s1"><span class="pl-c1">$</span>onRejected</span>);</pre> <p>Registers a rejection handler for promise. It is a shortcut for:</p> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span>-><span class="pl-en">then</span>(<span class="pl-c1">null</span>, <span class="pl-s1"><span class="pl-c1">$</span>onRejected</span>);</pre> <p>Additionally, you can type hint the <code>$reason</code> argument of <code>$onRejected</code> to catch only specific errors.</p> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span> -><span class="pl-en">catch</span>(<span class="pl-k">function</span> (<span class="pl-smi">\<span class="pl-smi">RuntimeException</span></span> <span class="pl-s1"><span class="pl-c1">$</span>reason</span>) { <span class="pl-c">// Only catch \RuntimeException instances</span> <span class="pl-c">// All other types of errors will propagate automatically</span> }) -><span class="pl-en">catch</span>(<span class="pl-k">function</span> (<span class="pl-smi">\<span class="pl-smi">Throwable</span></span> <span class="pl-s1"><span class="pl-c1">$</span>reason</span>) { <span class="pl-c">// Catch other errors</span> });</pre> <h4 class="heading-element">PromiseInterface::finally()</h4><a id="user-content-promiseinterfacefinally" class="anchor" href="#user-content-promiseinterfacefinally" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>newPromise</span> = <span class="pl-s1"><span class="pl-c1">$</span>promise</span>-><span class="pl-en">finally</span>(callable <span class="pl-s1"><span class="pl-c1">$</span>onFulfilledOrRejected</span>);</pre> <p>Allows you to execute "cleanup" type tasks in a promise chain.</p> <p>It arranges for <code>$onFulfilledOrRejected</code> to be called, with no arguments, when the promise is either fulfilled or rejected.</p> <ul> <li>If <code>$promise</code> fulfills, and <code>$onFulfilledOrRejected</code> returns successfully, <code>$newPromise</code> will fulfill with the same value as <code>$promise</code>.</li> <li>If <code>$promise</code> fulfills, and <code>$onFulfilledOrRejected</code> throws or returns a rejected promise, <code>$newPromise</code> will reject with the thrown exception or rejected promise's reason.</li> <li>If <code>$promise</code> rejects, and <code>$onFulfilledOrRejected</code> returns successfully, <code>$newPromise</code> will reject with the same reason as <code>$promise</code>.</li> <li>If <code>$promise</code> rejects, and <code>$onFulfilledOrRejected</code> throws or returns a rejected promise, <code>$newPromise</code> will reject with the thrown exception or rejected promise's reason.</li> </ul> <p><code>finally()</code> behaves similarly to the synchronous finally statement. When combined with <code>catch()</code>, <code>finally()</code> allows you to write code that is similar to the familiar synchronous catch/finally pair.</p> <p>Consider the following synchronous code:</p> <pre><span class="pl-k">try</span> { <span class="pl-k">return</span> <span class="pl-en">doSomething</span>(); } <span class="pl-k">catch</span> (<span class="pl-smi">\<span class="pl-smi">Throwable</span></span> <span class="pl-s1"><span class="pl-c1">$</span>e</span>) { <span class="pl-k">return</span> <span class="pl-en">handleError</span>(<span class="pl-s1"><span class="pl-c1">$</span>e</span>); } <span class="pl-k">finally</span> { <span class="pl-en">cleanup</span>(); }</pre> <p>Similar asynchronous code (with <code>doSomething()</code> that returns a promise) can be written:</p> <pre><span class="pl-k">return</span> <span class="pl-en">doSomething</span>() -><span class="pl-en">catch</span>(<span class="pl-s">'<span class="pl-s">handleError</span>'</span>) -><span class="pl-en">finally</span>(<span class="pl-s">'<span class="pl-s">cleanup</span>'</span>);</pre> <h4 class="heading-element">PromiseInterface::cancel()</h4><a id="user-content-promiseinterfacecancel" class="anchor" href="#user-content-promiseinterfacecancel" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span>-><span class="pl-en">cancel</span>();</pre> <p>The <code>cancel()</code> method notifies the creator of the promise that there is no further interest in the results of the operation.</p> <p>Once a promise is settled (either fulfilled or rejected), calling <code>cancel()</code> on a promise has no effect.</p> <h4 class="heading-element"><del>PromiseInterface::otherwise()</del></h4><a id="user-content-promiseinterfaceotherwise" class="anchor" href="#user-content-promiseinterfaceotherwise" rel="nofollow noindex noopener external ugc"></a> <blockquote> <p>Deprecated since v3.0.0, see <a href="#user-content-promiseinterfacecatch" rel="nofollow noindex noopener external ugc"><code>catch()</code></a> instead.</p> </blockquote> <p>The <code>otherwise()</code> method registers a rejection handler for a promise.</p> <p>This method continues to exist only for BC reasons and to ease upgrading between versions. It is an alias for:</p> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span>-><span class="pl-en">catch</span>(<span class="pl-s1"><span class="pl-c1">$</span>onRejected</span>);</pre> <h4 class="heading-element"><del>PromiseInterface::always()</del></h4><a id="user-content-promiseinterfacealways" class="anchor" href="#user-content-promiseinterfacealways" rel="nofollow noindex noopener external ugc"></a> <blockquote> <p>Deprecated since v3.0.0, see <a href="#user-content-promiseinterfacefinally" rel="nofollow noindex noopener external ugc"><code>finally()</code></a> instead.</p> </blockquote> <p>The <code>always()</code> method allows you to execute "cleanup" type tasks in a promise chain.</p> <p>This method continues to exist only for BC reasons and to ease upgrading between versions. It is an alias for:</p> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span>-><span class="pl-en">finally</span>(<span class="pl-s1"><span class="pl-c1">$</span>onFulfilledOrRejected</span>);</pre> <h3 class="heading-element">Promise</h3><a id="user-content-promise-2" class="anchor" href="#user-content-promise-2" rel="nofollow noindex noopener external ugc"></a> <p>Creates a promise whose state is controlled by the functions passed to <code>$resolver</code>.</p> <pre><span class="pl-s1"><span class="pl-c1">$</span>resolver</span> = <span class="pl-k">function</span> (<span class="pl-smi">callable</span> <span class="pl-s1"><span class="pl-c1">$</span>resolve</span>, <span class="pl-smi">callable</span> <span class="pl-s1"><span class="pl-c1">$</span>reject</span>) { <span class="pl-c">// Do some work, possibly asynchronously, and then</span> <span class="pl-c">// resolve or reject.</span> <span class="pl-s1"><span class="pl-c1">$</span>resolve</span>(<span class="pl-s1"><span class="pl-c1">$</span>awesomeResult</span>); <span class="pl-c">// or throw new Exception('Promise rejected');</span> <span class="pl-c">// or $resolve($anotherPromise);</span> <span class="pl-c">// or $reject($nastyError);</span> }; <span class="pl-s1"><span class="pl-c1">$</span>canceller</span> = <span class="pl-k">function</span> () { <span class="pl-c">// Cancel/abort any running operations like network connections, streams etc.</span> <span class="pl-c">// Reject promise by throwing an exception</span> <span class="pl-k">throw</span> <span class="pl-k">new</span> <span class="pl-v">Exception</span>(<span class="pl-s">'<span class="pl-s">Promise cancelled</span>'</span>); }; <span class="pl-s1"><span class="pl-c1">$</span>promise</span> = <span class="pl-k">new</span> <span class="pl-v">React</span>\<span class="pl-v">Promise</span>\<span class="pl-v">Promise</span>(<span class="pl-s1"><span class="pl-c1">$</span>resolver</span>, <span class="pl-s1"><span class="pl-c1">$</span>canceller</span>);</pre> <p>The promise constructor receives a resolver function and an optional canceller function which both will be called with two arguments:</p> <ul> <li><code>$resolve($value)</code> - Primary function that seals the fate of the returned promise. Accepts either a non-promise value, or another promise. When called with a non-promise value, fulfills promise with that value. When called with another promise, e.g. <code>$resolve($otherPromise)</code>, promise's fate will be equivalent to that of <code>$otherPromise</code>.</li> <li><code>$reject($reason)</code> - Function that rejects the promise. It is recommended to just throw an exception instead of using <code>$reject()</code>.</li> </ul> <p>If the resolver or canceller throw an exception, the promise will be rejected with that thrown exception as the rejection reason.</p> <p>The resolver function will be called immediately, the canceller function only once all consumers called the <code>cancel()</code> method of the promise.</p> <h3 class="heading-element">Functions</h3><a id="user-content-functions" class="anchor" href="#user-content-functions" rel="nofollow noindex noopener external ugc"></a> <p>Useful functions for creating and joining collections of promises.</p> <p>All functions working on promise collections (like <code>all()</code>, <code>race()</code>, etc.) support cancellation. This means, if you call <code>cancel()</code> on the returned promise, all promises in the collection are cancelled.</p> <h4 class="heading-element">resolve()</h4><a id="user-content-resolve" class="anchor" href="#user-content-resolve" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span> = <span class="pl-en"><span class="pl-v">React</span>\<span class="pl-v">Promise</span>\resolve</span>(mixed <span class="pl-s1"><span class="pl-c1">$</span>promiseOrValue</span>);</pre> <p>Creates a promise for the supplied <code>$promiseOrValue</code>.</p> <p>If <code>$promiseOrValue</code> is a value, it will be the resolution value of the returned promise.</p> <p>If <code>$promiseOrValue</code> is a thenable (any object that provides a <code>then()</code> method), a trusted promise that follows the state of the thenable is returned.</p> <p>If <code>$promiseOrValue</code> is a promise, it will be returned as is.</p> <p>The resulting <code>$promise</code> implements the <a href="#user-content-promiseinterface" rel="nofollow noindex noopener external ugc"><code>PromiseInterface</code></a> and can be consumed like any other promise:</p> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span> = <span class="pl-en"><span class="pl-v">React</span>\<span class="pl-v">Promise</span>\resolve</span>(<span class="pl-c1">42</span>); <span class="pl-s1"><span class="pl-c1">$</span>promise</span>-><span class="pl-en">then</span>(<span class="pl-k">function</span> (<span class="pl-smi">int</span> <span class="pl-s1"><span class="pl-c1">$</span>result</span>): <span class="pl-smi">void</span> { <span class="pl-en">var_dump</span>(<span class="pl-s1"><span class="pl-c1">$</span>result</span>); }, <span class="pl-k">function</span> (<span class="pl-smi">\<span class="pl-smi">Throwable</span></span> <span class="pl-s1"><span class="pl-c1">$</span>e</span>): <span class="pl-smi">void</span> { <span class="pl-k">echo</span> <span class="pl-s">'<span class="pl-s">Error: </span>'</span> . <span class="pl-s1"><span class="pl-c1">$</span>e</span>-><span class="pl-en">getMessage</span>() . <span class="pl-c1">PHP_EOL</span>; });</pre> <h4 class="heading-element">reject()</h4><a id="user-content-reject" class="anchor" href="#user-content-reject" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span> = <span class="pl-en"><span class="pl-v">React</span>\<span class="pl-v">Promise</span>\reject</span>(\Throwable <span class="pl-s1"><span class="pl-c1">$</span>reason</span>);</pre> <p>Creates a rejected promise for the supplied <code>$reason</code>.</p> <p>Note that the <a href="https://www.php.net/manual/en/class.throwable.php" rel="nofollow noindex noopener external ugc"><code>\Throwable</code></a> interface introduced in PHP 7 covers both user land <a href="https://www.php.net/manual/en/class.exception.php" rel="nofollow noindex noopener external ugc"><code>\Exception</code></a>'s and <a href="https://www.php.net/manual/en/class.error.php" rel="nofollow noindex noopener external ugc"><code>\Error</code></a> internal PHP errors. By enforcing <code>\Throwable</code> as reason to reject a promise, any language error or user land exception can be used to reject a promise.</p> <p>The resulting <code>$promise</code> implements the <a href="#user-content-promiseinterface" rel="nofollow noindex noopener external ugc"><code>PromiseInterface</code></a> and can be consumed like any other promise:</p> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span> = <span class="pl-en"><span class="pl-v">React</span>\<span class="pl-v">Promise</span>\reject</span>(<span class="pl-k">new</span> <span class="pl-v">RuntimeException</span>(<span class="pl-s">'<span class="pl-s">Request failed</span>'</span>)); <span class="pl-s1"><span class="pl-c1">$</span>promise</span>-><span class="pl-en">then</span>(<span class="pl-k">function</span> (<span class="pl-smi">int</span> <span class="pl-s1"><span class="pl-c1">$</span>result</span>): <span class="pl-smi">void</span> { <span class="pl-en">var_dump</span>(<span class="pl-s1"><span class="pl-c1">$</span>result</span>); }, <span class="pl-k">function</span> (<span class="pl-smi">\<span class="pl-smi">Throwable</span></span> <span class="pl-s1"><span class="pl-c1">$</span>e</span>): <span class="pl-smi">void</span> { <span class="pl-k">echo</span> <span class="pl-s">'<span class="pl-s">Error: </span>'</span> . <span class="pl-s1"><span class="pl-c1">$</span>e</span>-><span class="pl-en">getMessage</span>() . <span class="pl-c1">PHP_EOL</span>; });</pre> <p>Note that rejected promises should always be handled similar to how any exceptions should always be caught in a <code>try</code> + <code>catch</code> block. If you remove the last reference to a rejected promise that has not been handled, it will report an unhandled promise rejection:</p> <pre><span class="pl-k">function</span> <span class="pl-en">incorrect</span>(): <span class="pl-smi">int</span> { <span class="pl-s1"><span class="pl-c1">$</span>promise</span> = <span class="pl-en"><span class="pl-v">React</span>\<span class="pl-v">Promise</span>\reject</span>(<span class="pl-k">new</span> <span class="pl-v">RuntimeException</span>(<span class="pl-s">'<span class="pl-s">Request failed</span>'</span>)); <span class="pl-c">// Commented out: No rejection handler registered here.</span> <span class="pl-c">// $promise->then(null, function (\Throwable $e): void { /* ignore */ });</span> <span class="pl-c">// Returning from a function will remove all local variable references, hence why</span> <span class="pl-c">// this will report an unhandled promise rejection here.</span> <span class="pl-k">return</span> <span class="pl-c1">42</span>; } <span class="pl-c">// Calling this function will log an error message plus its stack trace:</span> <span class="pl-c">// Unhandled promise rejection with RuntimeException: Request failed in example.php:10</span> <span class="pl-en">incorrect</span>();</pre> <p>A rejected promise will be considered "handled" if you catch the rejection reason with either the <a href="#user-content-promiseinterfacethen" rel="nofollow noindex noopener external ugc"><code>then()</code> method</a>, the <a href="#user-content-promiseinterfacecatch" rel="nofollow noindex noopener external ugc"><code>catch()</code> method</a>, or the <a href="#user-content-promiseinterfacefinally" rel="nofollow noindex noopener external ugc"><code>finally()</code> method</a>. Note that each of these methods return a new promise that may again be rejected if you re-throw an exception.</p> <p>A rejected promise will also be considered "handled" if you abort the operation with the <a href="#user-content-promiseinterfacecancel" rel="nofollow noindex noopener external ugc"><code>cancel()</code> method</a> (which in turn would usually reject the promise if it is still pending).</p> <p>See also the <a href="#user-content-set_rejection_handler" rel="nofollow noindex noopener external ugc"><code>set_rejection_handler()</code> function</a>.</p> <h4 class="heading-element">all()</h4><a id="user-content-all" class="anchor" href="#user-content-all" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span> = <span class="pl-en"><span class="pl-v">React</span>\<span class="pl-v">Promise</span>\all</span>(iterable <span class="pl-s1"><span class="pl-c1">$</span>promisesOrValues</span>);</pre> <p>Returns a promise that will resolve only once all the items in <code>$promisesOrValues</code> have resolved. The resolution value of the returned promise will be an array containing the resolution values of each of the items in <code>$promisesOrValues</code>.</p> <h4 class="heading-element">race()</h4><a id="user-content-race" class="anchor" href="#user-content-race" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span> = <span class="pl-en"><span class="pl-v">React</span>\<span class="pl-v">Promise</span>\race</span>(iterable <span class="pl-s1"><span class="pl-c1">$</span>promisesOrValues</span>);</pre> <p>Initiates a competitive race that allows one winner. Returns a promise which is resolved in the same way the first settled promise resolves.</p> <p>The returned promise will become <strong>infinitely pending</strong> if <code>$promisesOrValues</code> contains 0 items.</p> <h4 class="heading-element">any()</h4><a id="user-content-any" class="anchor" href="#user-content-any" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-s1"><span class="pl-c1">$</span>promise</span> = <span class="pl-en"><span class="pl-v">React</span>\<span class="pl-v">Promise</span>\any</span>(iterable <span class="pl-s1"><span class="pl-c1">$</span>promisesOrValues</span>);</pre> <p>Returns a promise that will resolve when any one of the items in <code>$promisesOrValues</code> resolves. The resolution value of the returned promise will be the resolution value of the triggering item.</p> <p>The returned promise will only reject if <em>all</em> items in <code>$promisesOrValues</code> are rejected. The rejection value will be a <code>React\Promise\Exception\CompositeException</code> which holds all rejection reasons. The rejection reasons can be obtained with <code>CompositeException::getThrowables()</code>.</p> <p>The returned promise will also reject with a <code>React\Promise\Exception\LengthException</code> if <code>$promisesOrValues</code> contains 0 items.</p> <h4 class="heading-element">set_rejection_handler()</h4><a id="user-content-set_rejection_handler" class="anchor" href="#user-content-set_rejection_handler" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-v">React</span>\<span class="pl-v">Promise</span>\set_rejection_handler(?callable <span class="pl-s1"><span class="pl-c1">$</span>callback</span>): ?callable;</pre> <p>Sets the global rejection handler for unhandled promise rejections.</p> <p>Note that rejected promises should always be handled similar to how any exceptions should always be caught in a <code>try</code> + <code>catch</code> block. If you remove the last reference to a rejected promise that has not been handled, it will report an unhandled promise rejection. See also the <a href="#user-content-reject" rel="nofollow noindex noopener external ugc"><code>reject()</code> function</a> for more details.</p> <p>The <code>?callable $callback</code> argument MUST be a valid callback function that accepts a single <code>Throwable</code> argument or a <code>null</code> value to restore the default promise rejection handler. The return value of the callback function will be ignored and has no effect, so you SHOULD return a <code>void</code> value. The callback function MUST NOT throw or the program will be terminated with a fatal error.</p> <p>The function returns the previous rejection handler or <code>null</code> if using the default promise rejection handler.</p> <p>The default promise rejection handler will log an error message plus its stack trace:</p> <pre><span class="pl-c">// Unhandled promise rejection with RuntimeException: Unhandled in example.php:2</span> <span class="pl-en"><span class="pl-v">React</span>\<span class="pl-v">Promise</span>\reject</span>(<span class="pl-k">new</span> <span class="pl-v">RuntimeException</span>(<span class="pl-s">'<span class="pl-s">Unhandled</span>'</span>));</pre> <p>The promise rejection handler may be used to use customize the log message or write to custom log targets. As a rule of thumb, this function should only be used as a last resort and promise rejections are best handled with either the <a href="#user-content-promiseinterfacethen" rel="nofollow noindex noopener external ugc"><code>then()</code> method</a>, the <a href="#user-content-promiseinterfacecatch" rel="nofollow noindex noopener external ugc"><code>catch()</code> method</a>, or the <a href="#user-content-promiseinterfacefinally" rel="nofollow noindex noopener external ugc"><code>finally()</code> method</a>. See also the <a href="#user-content-reject" rel="nofollow noindex noopener external ugc"><code>reject()</code> function</a> for more details.</p> <h2 class="heading-element">Examples</h2><a id="user-content-examples" class="anchor" href="#user-content-examples" rel="nofollow noindex noopener external ugc"></a> <h3 class="heading-element">How to use Deferred</h3><a id="user-content-how-to-use-deferred" class="anchor" href="#user-content-how-to-use-deferred" rel="nofollow noindex noopener external ugc"></a> <pre><span class="pl-k">function</span> <span class="pl-en">getAwesomeResultPromise</span>() { <span class="pl-s1"><span class="pl-c1">$</span>deferred</span> = <span class="pl-k">new</span> <span class="pl-v">React</span>\<span class="pl-v">Promise</span>\<span class="pl-v">Deferred</span>(); <span class="pl-c">// Execute a Node.js-style function using the callback pattern</span> <span class="pl-en">computeAwesomeResultAsynchronously</span>(<span class="pl-k">function</span> (<span class="pl-smi">\<span class="pl-smi">Throwable</span></span> <span class="pl-s1"><span class="pl-c1">$</span>error</span>, <span class="pl-s1"><span class="pl-c1">$</span>result</span>) <span class="pl-k">use</span> (<span class="pl-s1"><span class="pl-c1">$</span>deferred</span>) { <span class="pl-k">if</span> (<span class="pl-s1"><span class="pl-c1">$</span>error</span>) { <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">reject</span>(<span class="pl-s1"><span class="pl-c1">$</span>error</span>); } <span class="pl-k">else</span> { <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">resolve</span>(<span class="pl-s1"><span class="pl-c1">$</span>result</span>); } }); <span class="pl-c">// Return the promise</span> <span class="pl-k">return</span> <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">promise</span>(); } <span class="pl-en">getAwesomeResultPromise</span>() -><span class="pl-en">then</span>( <span class="pl-k">function</span> (<span class="pl-s1"><span class="pl-c1">$</span>value</span>) { <span class="pl-c">// Deferred resolved, do something with $value</span> }, <span class="pl-k">function</span> (<span class="pl-smi">\<span class="pl-smi">Throwable</span></span> <span class="pl-s1"><span class="pl-c1">$</span>reason</span>) { <span class="pl-c">// Deferred rejected, do something with $reason</span> } );</pre> <h3 class="heading-element">How promise forwarding works</h3><a id="user-content-how-promise-forwarding-works" class="anchor" href="#user-content-how-promise-forwarding-works" rel="nofollow noindex noopener external ugc"></a> <p>A few simple examples to show how the mechanics of Promises/A forwarding works. These examples are contrived, of course, and in real usage, promise chains will typically be spread across several function calls, or even several levels of your application architecture.</p> <h4 class="heading-element">Resolution forwarding</h4><a id="user-content-resolution-forwarding" class="anchor" href="#user-content-resolution-forwarding" rel="nofollow noindex noopener external ugc"></a> <p>Resolved promises forward resolution values to the next promise. The first promise, <code>$deferred->promise()</code>, will resolve with the value passed to <code>$deferred->resolve()</code> below.</p> <p>Each call to <code>then()</code> returns a new promise that will resolve with the return value of the previous handler. This creates a promise "pipeline".</p> <pre><span class="pl-s1"><span class="pl-c1">$</span>deferred</span> = <span class="pl-k">new</span> <span class="pl-v">React</span>\<span class="pl-v">Promise</span>\<span class="pl-v">Deferred</span>(); <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">promise</span>() -><span class="pl-en">then</span>(<span class="pl-k">function</span> (<span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-c">// $x will be the value passed to $deferred->resolve() below</span> <span class="pl-c">// and returns a *new promise* for $x + 1</span> <span class="pl-k">return</span> <span class="pl-s1"><span class="pl-c1">$</span>x</span> + <span class="pl-c1">1</span>; }) -><span class="pl-en">then</span>(<span class="pl-k">function</span> (<span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-c">// $x === 2</span> <span class="pl-c">// This handler receives the return value of the</span> <span class="pl-c">// previous handler.</span> <span class="pl-k">return</span> <span class="pl-s1"><span class="pl-c1">$</span>x</span> + <span class="pl-c1">1</span>; }) -><span class="pl-en">then</span>(<span class="pl-k">function</span> (<span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-c">// $x === 3</span> <span class="pl-c">// This handler receives the return value of the</span> <span class="pl-c">// previous handler.</span> <span class="pl-k">return</span> <span class="pl-s1"><span class="pl-c1">$</span>x</span> + <span class="pl-c1">1</span>; }) -><span class="pl-en">then</span>(<span class="pl-k">function</span> (<span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-c">// $x === 4</span> <span class="pl-c">// This handler receives the return value of the</span> <span class="pl-c">// previous handler.</span> <span class="pl-k">echo</span> <span class="pl-s">'<span class="pl-s">Resolve </span>'</span> . <span class="pl-s1"><span class="pl-c1">$</span>x</span>; }); <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">resolve</span>(<span class="pl-c1">1</span>); <span class="pl-c">// Prints "Resolve 4"</span></pre> <h4 class="heading-element">Rejection forwarding</h4><a id="user-content-rejection-forwarding" class="anchor" href="#user-content-rejection-forwarding" rel="nofollow noindex noopener external ugc"></a> <p>Rejected promises behave similarly, and also work similarly to try/catch: When you catch an exception, you must rethrow for it to propagate.</p> <p>Similarly, when you handle a rejected promise, to propagate the rejection, "rethrow" it by either returning a rejected promise, or actually throwing (since promise translates thrown exceptions into rejections)</p> <pre><span class="pl-s1"><span class="pl-c1">$</span>deferred</span> = <span class="pl-k">new</span> <span class="pl-v">React</span>\<span class="pl-v">Promise</span>\<span class="pl-v">Deferred</span>(); <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">promise</span>() -><span class="pl-en">then</span>(<span class="pl-k">function</span> (<span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-k">throw</span> <span class="pl-k">new</span> \<span class="pl-v">Exception</span>(<span class="pl-s1"><span class="pl-c1">$</span>x</span> + <span class="pl-c1">1</span>); }) -><span class="pl-en">catch</span>(<span class="pl-k">function</span> (<span class="pl-smi">\<span class="pl-smi">Exception</span></span> <span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-c">// Propagate the rejection</span> <span class="pl-k">throw</span> <span class="pl-s1"><span class="pl-c1">$</span>x</span>; }) -><span class="pl-en">catch</span>(<span class="pl-k">function</span> (<span class="pl-smi">\<span class="pl-smi">Exception</span></span> <span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-c">// Can also propagate by returning another rejection</span> <span class="pl-k">return</span> <span class="pl-en"><span class="pl-v">React</span>\<span class="pl-v">Promise</span>\reject</span>( <span class="pl-k">new</span> \<span class="pl-v">Exception</span>(<span class="pl-s1"><span class="pl-c1">$</span>x</span>-><span class="pl-en">getMessage</span>() + <span class="pl-c1">1</span>) ); }) -><span class="pl-en">catch</span>(<span class="pl-k">function</span> (<span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-k">echo</span> <span class="pl-s">'<span class="pl-s">Reject </span>'</span> . <span class="pl-s1"><span class="pl-c1">$</span>x</span>-><span class="pl-en">getMessage</span>(); <span class="pl-c">// 3</span> }); <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">resolve</span>(<span class="pl-c1">1</span>); <span class="pl-c">// Prints "Reject 3"</span></pre> <h4 class="heading-element">Mixed resolution and rejection forwarding</h4><a id="user-content-mixed-resolution-and-rejection-forwarding" class="anchor" href="#user-content-mixed-resolution-and-rejection-forwarding" rel="nofollow noindex noopener external ugc"></a> <p>Just like try/catch, you can choose to propagate or not. Mixing resolutions and rejections will still forward handler results in a predictable way.</p> <pre><span class="pl-s1"><span class="pl-c1">$</span>deferred</span> = <span class="pl-k">new</span> <span class="pl-v">React</span>\<span class="pl-v">Promise</span>\<span class="pl-v">Deferred</span>(); <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">promise</span>() -><span class="pl-en">then</span>(<span class="pl-k">function</span> (<span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-k">return</span> <span class="pl-s1"><span class="pl-c1">$</span>x</span> + <span class="pl-c1">1</span>; }) -><span class="pl-en">then</span>(<span class="pl-k">function</span> (<span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-k">throw</span> <span class="pl-k">new</span> \<span class="pl-v">Exception</span>(<span class="pl-s1"><span class="pl-c1">$</span>x</span> + <span class="pl-c1">1</span>); }) -><span class="pl-en">catch</span>(<span class="pl-k">function</span> (<span class="pl-smi">\<span class="pl-smi">Exception</span></span> <span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-c">// Handle the rejection, and don't propagate.</span> <span class="pl-c">// This is like catch without a rethrow</span> <span class="pl-k">return</span> <span class="pl-s1"><span class="pl-c1">$</span>x</span>-><span class="pl-en">getMessage</span>() + <span class="pl-c1">1</span>; }) -><span class="pl-en">then</span>(<span class="pl-k">function</span> (<span class="pl-s1"><span class="pl-c1">$</span>x</span>) { <span class="pl-k">echo</span> <span class="pl-s">'<span class="pl-s">Mixed </span>'</span> . <span class="pl-s1"><span class="pl-c1">$</span>x</span>; <span class="pl-c">// 4</span> }); <span class="pl-s1"><span class="pl-c1">$</span>deferred</span>-><span class="pl-en">resolve</span>(<span class="pl-c1">1</span>); <span class="pl-c">// Prints "Mixed 4"</span></pre> <h2 class="heading-element">Install</h2><a id="user-content-install" class="anchor" href="#user-content-install" rel="nofollow noindex noopener external ugc"></a> <p>The recommended way to install this library is <a href="https://getcomposer.org/" rel="nofollow noindex noopener external ugc">through Composer</a>. <a href="https://getcomposer.org/doc/00-intro.md" rel="nofollow noindex noopener external ugc">New to Composer?</a></p> <p>This project follows <a href="https://semver.org/" rel="nofollow noindex noopener external ugc">SemVer</a>. This will install the latest supported version from this branch:</p> <pre>composer require react/promise:^3.2</pre> <p>See also the <a href="https://github.com/reactphp/promise/blob/HEAD/CHANGELOG.md" rel="nofollow noindex noopener external ugc">CHANGELOG</a> for details about version upgrades.</p> <p>This project aims to run on any platform and thus does not require any PHP extensions and supports running on PHP 7.1 through current PHP 8+. It's <em>highly recommended to use the latest supported PHP version</em> for this project.</p> <p>We're committed to providing long-term support (LTS) options and to provide a smooth upgrade path. If you're using an older PHP version, you may use the <a href="https://github.com/reactphp/promise/tree/2.x" rel="nofollow noindex noopener external ugc"><code>2.x</code> branch</a> (PHP 5.4+) or <a href="https://github.com/reactphp/promise/tree/1.x" rel="nofollow noindex noopener external ugc"><code>1.x</code> branch</a> (PHP 5.3+) which both provide a compatible API but do not take advantage of newer language features. You may target multiple versions at the same time to support a wider range of PHP versions like this:</p> <pre>composer require <span class="pl-s"><span class="pl-pds">"</span>react/promise:^3 || ^2 || ^1<span class="pl-pds">"</span></span></pre> <h2 class="heading-element">Tests</h2><a id="user-content-tests" class="anchor" href="#user-content-tests" rel="nofollow noindex noopener external ugc"></a> <p>To run the test suite, you first need to clone this repo and then install all dependencies <a href="https://getcomposer.org/" rel="nofollow noindex noopener external ugc">through Composer</a>:</p> <pre>composer install</pre> <p>To run the test suite, go to the project root and run:</p> <pre>vendor/bin/phpunit</pre> <p>On top of this, we use PHPStan on max level to ensure type safety across the project:</p> <pre>vendor/bin/phpstan</pre> <h2 class="heading-element">Credits</h2><a id="user-content-credits" class="anchor" href="#user-content-credits" rel="nofollow noindex noopener external ugc"></a> <p>Promise is a port of <a href="https://github.com/cujojs/when" rel="nofollow noindex noopener external ugc">when.js</a> by <a href="https://github.com/briancavalier" rel="nofollow noindex noopener external ugc">Brian Cavalier</a>.</p> <p>Also, large parts of the documentation have been ported from the when.js <a href="https://github.com/cujojs/when/wiki" rel="nofollow noindex noopener external ugc">Wiki</a> and the <a href="https://github.com/cujojs/when/blob/master/docs/api.md" rel="nofollow noindex noopener external ugc">API docs</a>.</p> <h2 class="heading-element">License</h2><a id="user-content-license" class="anchor" href="#user-content-license" rel="nofollow noindex noopener external ugc"></a> <p>Released under the <a href="https://github.com/reactphp/promise/blob/HEAD/LICENSE" rel="nofollow noindex noopener external ugc">MIT</a> license.</p> </div> </div> </div> </section> </section> </section> <footer class="wrapper-footer"> <nav class="container"> <div class="row"> <ul class="social col-xs-7 col-sm-4 col-md-2 pull-right"> <li><a href="http://github.com/composer/packagist" title="GitHub"><span class="icon-github"></span></a></li> <li><a href="https://twitter.com/packagist" title="Follow @packagist"><span class="icon-twitter"></span></a></li> <li><a href="mailto:contact@packagist.org" title="Contact"><span class="icon-mail"></span></a></li> </ul> <ul class="col-xs-4 col-sm-4 col-md-2"> <li><a href="/about">About Packagist</a></li> <li><a href="/feeds/" rel="nofollow">Atom/RSS Feeds</a></li> </ul> <div class="clearfix visible-xs-block"></div> <ul class="col-xs-3 col-sm-4 col-md-2"> <li><a href="/statistics" rel="nofollow">Statistics</a></li> <li><a href="/explore/">Browse Packages</a></li> </ul> <div class="clearfix visible-xs-block visible-sm-block"></div> <ul class="col-xs-3 col-sm-4 col-md-2"> <li><a href="/apidoc">API</a></li> <li><a href="/mirrors">Mirrors</a></li> </ul> <div class="clearfix visible-xs-block"></div> <ul class="col-xs-3 col-sm-4 col-md-2"> <li><a href="https://status.packagist.org/">Status</a></li> <li><a href="https://p.datadoghq.com/sb/x98w56x71erzshui-4a54c45f82bacc991e83302548934b6a">Dashboard</a></li> </ul> </div> <div class="row sponsor"> <p class="col-xs-12">Packagist maintenance and hosting provided by <a href="https://packagist.com/"><img alt="Private Packagist" src="/img/private-packagist.svg?v=1739794478" /></a></p> </div> <div class="row sponsor"> <p class="col-xs-12">Monitoring provided by <a href="https://datadog.com/"><img alt="Datadog" src="/img/datadog-light.png?v=1739794478" /></a></p> </div> </nav> </footer> <script nonce="VL78G2lBRZDnitlw9l5HJw=="> var algoliaConfig = {"app_id":"M58222SH95","search_key":"5ae4d03c98685bd7364c2e0fd819af05","index_name":"packagist"}; window.process = { env: { DEBUG: undefined }, }; </script> <script nonce="VL78G2lBRZDnitlw9l5HJw==" src="/build/app.js?v=1739794478"></script> </body> </html>