若是你還不太瞭解PHP下的生成器和協程,你能夠根據下面目錄翻閱php
│ │ autoload.php │ │ promise1.php │ │ promise2.php │ │ promise3.php │ │ promise4.php │ │ promise5.php │ │ │ └─classes │ Promise1.php │ Promise2.php │ Promise3.php │ Promise4.php │ Promise5.php │ PromiseState.php
classess/PromiseState.phphtml
final class PromiseState { const PENDING = 'pending'; const FULFILLED = 'fulfilled'; const REJECTED = 'rejected'; }
classess/Promise1.phpes6
// 嘗試一 class Promise1 { private $value; private $reason; private $state; public function __construct(\Closure $func = null) { $this->state = PromiseState::PENDING; $func([$this, 'resolve'], [$this, 'reject']); } /** * 執行回調方法裏的resolve綁定的方法 * @param null $value */ public function resolve($value = null) { // 回調執行resolve傳參的值,賦值給result $this->value = $value; if ($this->state == PromiseState::PENDING) { $this->state = PromiseState::FULFILLED; } } public function reject($reason = null) { // 回調執行resolve傳參的值,賦值給result $this->reason = $reason; if ($this->state == PromiseState::PENDING) { $this->state = PromiseState::REJECTED; } } public function getState() { return $this->state; } public function getValue() { return $this->value; } public function getReason() { return $this->reason; } }
promise1.phpsegmentfault
require "autoload.php"; $promise = new Promise1(function($resolve, $reject) { $resolve("打印我"); }); var_dump($promise->getState()); var_dump($promise->getValue());
string(9) "fulfilled" string(9) "打印我"
咱們在這裏建構了最基礎的Promise模型
classess/Promise2.php數組
<?php // 嘗試二 (增長鏈式then) class Promise2 { private $value; private $reason; private $state; public function __construct(\Closure $func = null) { $this->state = PromiseState::PENDING; $func([$this, 'resolve'], [$this, 'reject']); } public function then(\Closure $onFulfilled = null, \Closure $onRejected = null) { // 若是狀態是fulfilled,直接回調執行並傳參value if ($this->state == PromiseState::FULFILLED) { $onFulfilled($this->value); } // 若是狀態是rejected,直接回調執行並傳參reason if ($this->state == PromiseState::REJECTED) { $onRejected($this->reason); } // 返回對象自身,實現鏈式調用 return $this; } /** * 執行回調方法裏的resolve綁定的方法 * 本狀態只能從pending->fulfilled * @param null $value */ public function resolve($value = null) { if ($this->state == PromiseState::PENDING) { $this->state = PromiseState::FULFILLED; $this->value = $value; } } /** * 執行回調方法裏的rejected綁定的方法 * 本狀態只能從pending->rejected * @param null $reason */ public function reject($reason = null) { if ($this->state == PromiseState::PENDING) { $this->state = PromiseState::REJECTED; $this->reason = $reason; } } public function getState() { return $this->state; } public function getValue() { return $this->value; } public function getReason() { return $this->reason; } }
promise2.phppromise
<?php require "autoload.php"; $promise = new Promise2(function($resolve, $reject) { $resolve("打印我"); }); $promise->then(function ($value) { var_dump($value); }, function ($reason) { var_dump($reason); })->then(function ($value) { var_dump($value); }, function ($reason) { var_dump($reason); });
string(9) "打印我" string(9) "打印我"
咱們實現了鏈式then方法 若是咱們的構造裏的回調是異步執行的話,那麼狀態在沒有變成fulfilled以前,咱們then裏的回調方法就永遠無法執行
classess/Promise3.php異步
// 解決思路:咱們確定要把then傳入的回調,放到Promise構造裏回調代碼執行完後resolve調用後改變了state狀態後再調用,因此咱們必須存儲到一個地方並方便後續調用 // 咱們須要改造then、resolve和reject方法 class Promise3 { private $value; private $reason; private $state; private $fulfilledCallbacks = []; private $rejectedCallbacks = []; public function __construct(\Closure $func = null) { $this->state = PromiseState::PENDING; $func([$this, 'resolve'], [$this, 'reject']); } public function then(\Closure $onFulfilled = null, \Closure $onRejected = null) { // 若是是異步回調,狀態未變化以前,then的回調方法壓入相應的數組方便後續調用 if ($this->state == PromiseState::PENDING) { $this->fulfilledCallbacks[] = static function() use ($onFulfilled, $value){ $onFulfilled($this->value); }; $this->rejectedCallbacks[] = static function() use ($onRejected, $reason){ $onRejected($this->reason); }; } // 若是狀態是fulfilled,直接回調執行並傳參value if ($this->state == PromiseState::FULFILLED) { $onFulfilled($this->value); } // 若是狀態是rejected,直接回調執行並傳參reason if ($this->state == PromiseState::REJECTED) { $onRejected($this->reason); } // 返回對象自身,實現鏈式調用 return $this; } /** * 執行回調方法裏的resolve綁定的方法 * 本狀態只能從pending->fulfilled * @param null $value */ public function resolve($value = null) { if ($this->state == PromiseState::PENDING) { $this->state = PromiseState::FULFILLED; $this->value = $value; array_walk($this->fulfilledCallbacks, function ($callback) { $callback(); }); } } /** * 執行回調方法裏的rejected綁定的方法 * 本狀態只能從pending->rejected * @param null $reason */ public function reject($reason = null) { if ($this->state == PromiseState::PENDING) { $this->state = PromiseState::REJECTED; $this->reason = $reason; } } public function getState() { return $this->state; } public function getValue() { return $this->value; } public function getReason() { return $this->reason; } }
promise3.phpui
require "autoload.php"; $promise = new Promise3(function($resolve, $reject) { $resolve("打印我"); }); $promise->then(function ($value) { var_dump($value); }, function ($reason) { var_dump($reason); })->then(function ($value) { var_dump($value); }, function ($reason) { var_dump($reason); });
string(9) "打印我" string(9) "打印我"
咱們此次基本實現了真正的鏈式then方法 不過在Promise/A+裏規範,要求then返回每次都要求是一個新的Promise對象 then方法成功執行,至關於返回一個實例一個Promise回調裏執行resolve方法,resolve值爲then裏return的值 then方法執行失敗或出錯,至關於返回一個實例一個Promise回調裏執行rejected方法,rejected值爲then裏return的值
classess/Promise4.phpthis
class Promise4 { private $value; private $reason; private $state; private $fulfilledCallbacks = []; private $rejectedCallbacks = []; public function __construct(\Closure $func = null) { $this->state = PromiseState::PENDING; $func([$this, 'resolve'], [$this, 'reject']); } public function then(\Closure $onFulfilled = null, \Closure $onRejected = null) { $thenPromise = new Promise4(function ($reslove, $reject) use (&$thenPromise, $onFulfilled, $onRejected) { //$this 表明的當前的Promise對象,不要混淆了 // 若是是異步回調,狀態未變化以前,then的回調方法壓入相應的數組方便後續調用 if ($this->state == PromiseState::PENDING) { $this->fulfilledCallbacks[] = static function() use ($thenPromise, $onFulfilled, $reslove, $reject){ $value = $onFulfilled($this->value); $this->resolvePromise($thenPromise, $value, $reslove, $reject); }; $this->rejectedCallbacks[] = static function() use ($thenPromise, $onRejected, $reslove, $reject){ $reason = $onRejected($this->reason); $this->resolvePromise($thenPromise, $reason, $reslove, $reject); }; } // 若是狀態是fulfilled,直接回調執行並傳參value if ($this->state == PromiseState::FULFILLED) { $value = $onFulfilled($this->value); $this->resolvePromise($thenPromise, $value, $reslove, $reject); } // 若是狀態是rejected,直接回調執行並傳參reason if ($this->state == PromiseState::REJECTED) { $reason = $onRejected($this->reason); $this->resolvePromise($thenPromise, $reason, $reslove, $reject); } }); // 返回對象自身,實現鏈式調用 return $thenPromise; } /** * 解決Pormise鏈式then傳遞 * 可參考 [Promises/A+]2.3 [https://promisesaplus.com/#the-promise-resolution-procedure] * @param $thenPromise * @param $x $x爲thenable對象 * @param $resolve * @param $reject */ private function resolvePromise($thenPromise, $x, $resolve, $reject) { $called = false; if ($thenPromise === $x) { return $reject(new \Exception('循環引用')); } if ( is_object($x) && method_exists($x, 'then')) { $resolveCb = function ($value) use($thenPromise, $resolve, $reject, $called) { if ($called) return ; $called = true; // 成功值y有可能仍是promise或者是具備then方法等,再次resolvePromise,直到成功值爲基本類型或者非thenable $this->resolvePromise($thenPromise, $value, $resolve, $reject); }; $rejectCb = function($reason) use($thenPromise, $resolve, $reject, $called) { if ($called) return ; $called = true; $reject($reason); }; call_user_func_array([$x, 'then'], [$resolveCb, $rejectCb]); } else { if ($called) return ; $called = true; $resolve($x); } } /** * 執行回調方法裏的resolve綁定的方法 * 本狀態只能從pending->fulfilled * @param null $value */ public function resolve($value = null) { if ($this->state == PromiseState::PENDING) { $this->state = PromiseState::FULFILLED; $this->value = $value; array_walk($this->fulfilledCallbacks, function ($callback) { $callback(); }); } } /** * 執行回調方法裏的rejected綁定的方法 * 本狀態只能從pending->rejected * @param null $reason */ public function reject($reason = null) { if ($this->state == PromiseState::PENDING) { $this->state = PromiseState::REJECTED; $this->reason = $reason; } } public function getState() { return $this->state; } public function getValue() { return $this->value; } public function getReason() { return $this->reason; } }
promise4.phpcode
require "autoload.php"; $promise1 = new Promise4(function($resolve, $reject) { $resolve("打印我"); }); $promise2 = $promise1->then(function ($value) { var_dump($value); return "promise2"; }, function ($reason) { var_dump($reason); }); $promise3 = $promise2->then(function ($value) { var_dump($value); return new Promise4(function($resolve, $reject) { $resolve("promise3"); }); }, function ($reason) { var_dump($reason); }); $promise4 = $promise3->then(function ($value) { var_dump($value); return "promise4"; }, function ($reason) { var_dump($reason); }); var_dump($promise4);
string(9) "打印我" string(8) "promise2" string(8) "promise3" object(Promise4)#15 (5) { ["value":"Promise4":private]=> string(8) "promise4" ["reason":"Promise4":private]=> NULL ["state":"Promise4":private]=> string(9) "fulfilled" ["fulfilledCallbacks":"Promise4":private]=> array(0) { } ["rejectedCallbacks":"Promise4":private]=> array(0) { } }
一個基本的Pormise,不過咱們上面都是基於成功fulfilled狀態的實現 下面咱們來增長錯誤捕獲
classess/Promise5.php
class Promise5 { private $value; private $reason; private $state; private $fulfilledCallbacks = []; private $rejectedCallbacks = []; public function __construct(\Closure $func = null) { $this->state = PromiseState::PENDING; $func([$this, 'resolve'], [$this, 'reject']); } public function then(\Closure $onFulfilled = null, \Closure $onRejected = null) { // 此處做用是兼容then方法的如下四種參數變化,catchError就是第二種狀況 // 1. then($onFulfilled, null) // 2. then(null, $onRejected) // 3. then(null, null) // 4. then($onFulfilled, $onRejected) $onFulfilled = is_callable($onFulfilled) ? $onFulfilled : function ($value) {return $value;}; $onRejected = is_callable($onRejected) ? $onRejected : function ($reason) {throw $reason;}; $thenPromise = new Promise5(function ($reslove, $reject) use (&$thenPromise, $onFulfilled, $onRejected) { //$this 表明的當前的Promise對象,不要混淆了 // 若是是異步回調,狀態未變化以前,then的回調方法壓入相應的數組方便後續調用 if ($this->state == PromiseState::PENDING) { $this->fulfilledCallbacks[] = static function() use ($thenPromise, $onFulfilled, $reslove, $reject){ try { $value = $onFulfilled($this->value); $this->resolvePromise($thenPromise, $value, $reslove, $reject); } catch (\Exception $e) { $reject($e); } }; $this->rejectedCallbacks[] = static function() use ($thenPromise, $onRejected, $reslove, $reject){ try { $reason = $onRejected($this->reason); $this->resolvePromise($thenPromise, $reason, $reslove, $reject); } catch (\Exception $e) { $reject($e); } }; } // 若是狀態是fulfilled,直接回調執行並傳參value if ($this->state == PromiseState::FULFILLED) { try { $value = $onFulfilled($this->value); $this->resolvePromise($thenPromise, $value, $reslove, $reject); } catch (\Exception $e) { $reject($e); } } // 若是狀態是rejected,直接回調執行並傳參reason if ($this->state == PromiseState::REJECTED) { try { $reason = $onRejected($this->reason); $this->resolvePromise($thenPromise, $reason, $reslove, $reject); } catch (\Exception $e) { $reject($e); } } }); // 返回對象自身,實現鏈式調用 return $thenPromise; } public function catchError($onRejected) { return $this->then(null, $onRejected); } /** * 解決Pormise鏈式then傳遞 * 可參考 [Promises/A+]2.3 [https://promisesaplus.com/#the-promise-resolution-procedure] * @param $thenPromise * @param $x $x爲thenable對象 * @param $resolve * @param $reject */ private function resolvePromise($thenPromise, $x, $resolve, $reject) { $called = false; if ($thenPromise === $x) { return $reject(new \Exception('循環引用')); } if ( is_object($x) && method_exists($x, 'then')) { try { $resolveCb = function ($value) use ($thenPromise, $resolve, $reject, $called) { if ($called) return; $called = true; // 成功值y有可能仍是promise或者是具備then方法等,再次resolvePromise,直到成功值爲基本類型或者非thenable $this->resolvePromise($thenPromise, $value, $resolve, $reject); }; $rejectCb = function ($reason) use ($thenPromise, $resolve, $reject, $called) { if ($called) return; $called = true; $reject($reason); }; call_user_func_array([$x, 'then'], [$resolveCb, $rejectCb]); } catch (\Exception $e) { if ($called) return ; $called = true; $reject($e); } } else { if ($called) return ; $called = true; $resolve($x); } } /** * 執行回調方法裏的resolve綁定的方法 * 本狀態只能從pending->fulfilled * @param null $value */ public function resolve($value = null) { if ($this->state == PromiseState::PENDING) { $this->state = PromiseState::FULFILLED; $this->value = $value; array_walk($this->fulfilledCallbacks, function ($callback) { $callback(); //由於回調自己攜帶了做用於,因此直接調用,沒法參數 }); } } /** * 執行回調方法裏的rejected綁定的方法 * 本狀態只能從pending->rejected * @param null $reason */ public function reject($reason = null) { if ($this->state == PromiseState::PENDING) { $this->state = PromiseState::REJECTED; $this->reason = $reason; array_walk($this->rejectedCallbacks, function ($callback) { $callback(); //由於回調自己攜帶了做用於,因此直接調用,沒法參數 }); } } public function getState() { return $this->state; } public function getValue() { return $this->value; } public function getReason() { return $this->reason; } }
promise5.php
require "autoload.php"; $promise1 = new Promise5(function($resolve, $reject) { $resolve("打印我"); }); $promise2 = $promise1->then(function ($value) { var_dump($value); throw new \Exception("promise2 error"); return "promise2"; }, function ($reason) { var_dump($reason->getMessage()); return "promise3 error return"; }); //咱們能夠簡寫then方法,只傳入$onFulfilled方法,而後錯誤會本身冒泡方式到下一個catchError或then裏處理。 //$promise3 = $promise2->then(function ($value) { // var_dump($value); // return new Promise5(function($resolve, $reject) { // $resolve("promise3"); // }); //})->catchError(function ($reason) { // var_dump($reason->getMessage()); // return "promise3 error return"; //}); $promise3 = $promise2->then(function ($value) { var_dump($value); return new Promise5(function($resolve, $reject) { $resolve("promise3"); }); }, function ($reason) { var_dump($reason->getMessage()); return "promise3 error return"; }); $promise4 = $promise3->then(function ($value) { var_dump($value); return "promise4"; }, function ($reason) { echo $reason->getMessage(); }); var_dump($promise4);
string(9) "打印我" string(14) "promise2 error" string(21) "promise3 error return" object(Promise4)#10 (5) { ["value":"Promise4":private]=> string(8) "promise4" ["reason":"Promise4":private]=> NULL ["state":"Promise4":private]=> string(9) "fulfilled" ["fulfilledCallbacks":"Promise4":private]=> array(0) { } ["rejectedCallbacks":"Promise4":private]=> array(0) { } }
這裏咱們基礎實現了一個能夠用於生產環境的Promise 後續咱們會接續完善這個Promise的特有方法,好比:finally, all, race, resolve, reject等 後續再介紹用Promise實現的自動執行器等