You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<script>
(function () {functionmap1(data){throw[newError('map1.error'),data];returndata+1;}functionmap2(data){// throw [ new Error('map2.error'), data ];returndata+1;}functionmap3(data){throw[newError('map3.error'),data];returndata+1;}constpf=(()=>newPromise((ok)=>{throw[newError('p.error'),1];ok(1);}));pf().catch(([e,data])=>{console.error(e);returndata;}).then(map1).catch(([e,data])=>{console.error(e);returndata;}).then(map2).catch(([e,data])=>{console.error(e);returndata;}).then(map3).catch(([e,data])=>{console.error(e);returndata;}).then(console.info).catch(([e,data])=>{console.error(e);returndata;});})();
</script>
Look the code in PHP:
<?phpuseGuzzleHttp\Promise\Promise;
require_once__DIR__ . '/vendor/autoload.php';
classMyExceptionextends \RuntimeException
{
protected$data;
publicfunction__construct(
array$messageData,
$code = 0,
Throwable$previous = null
)
{
[ $message, $data ] = $messageData;
$this->data = $data;
parent::__construct($message, $code, $previous);
}
publicfunctiongetData()
{
return$this->data;
}
}
functionmap1($data)
{
thrownewMyException([ 'map1.error', $data ]);
return$data + 1;
}
functionmap2($data)
{
thrownewMyException([ 'map2.error', $data ]);
return$data + 1;
}
functionmap3($data)
{
thrownewMyException([ 'map3.error', $data ]);
return$data + 1;
}
functiononError(\Throwable$e)
{
$data = null;
if ($e instanceof MyException) {
$data = $e->getData();
}
var_dump($e);
return$data;
}
$pf = function () : Promise {
return$promise = newPromise(function () use (&$promise) {
thrownewMyException([ 'p.error', 1 ]);
$promise->resolve(1);
});
};
$promise = $pf();
$promise
->then('map1', 'onError')
->then('map2', 'onError')
->then('map3', 'onError')
;
$promise->wait(); // throws an exception... breaks your pipeline at all// requires to try/catch block everywhere in the code and makes your library like "async" call support (that could be made with \Generator that is bundled)// so you created Factory for Deferreds with uncomfortable async support because the php is a heck for now...
Reasons:
Promise runs immediately and takes as argument $fnOk and $fnFail, so if you want to call it later - you wrap "new Promise" to function, that gives you the Task (delayed call)
Promise has the CHAINED call ->catch(), allows you to handle error, and then CONTINUE chain or if you define catch block near original call you could define retry step right there
Javascript allows you to throw array instead of wrap exception in exception, so this FEATURE you have to realize manually to simplify code-(reading|writing), you need to create your own SUGAR, because PHP-devs is trapped to community voting idea (that day-to-day signalled about php is died forever)
Yes, Javascript has a problem that initially promise CANNOT be resolved outside init function, so it requires to create Deferred as a Promise decorator. Guess you need to realize both of them, cus of Deferreds is needed to connect two modules via Event, instead Promise - gives you chained (and possible async) calling of your pipeline.
Main idea of whole code in the world "do not change library code but allow me to customise behavior of it and save my written code almost untouched." It usually could be solved in OOP paradigm with decorating classes methods etc, but mass problem of OOP is a VERY SMALL METHODS that is VERY MANY COUNT of. Fast refactoring requires change few lines of existing code, but OOP forces to rewrite all. Thats why the Pipeline idea (that is used in raw electricity btw) is a required stuff, if you write your controllers in pipeline paradigm you dont change even count of spaces, dont add any curly braces or something, you add catch block for problem place and you're done.
So your library is a Deferred factory without exception catching and without known sugar for js devs.
And can be simplified with:
functioncontroller()
{
$readln = false;
foreach ($gen = chainedAction($readln) as$event => $eventData) {
$readln = false;
if ($event === 'state1') {
(function ($eventData) { /* ...do some side effect (event-handler) */ })($eventData);
} elseif ($event === 'state2') {
$readln = true;
$gen->send($eventData + 2); // ...prepare data to return it back (pipeline/command-handler)
}
}
}
functionchainedAction(bool &$readln) : iterable
{
$data = 0;
yield 'state1' => $data;
if ($readln) $data = yield;
$data++;
yield 'state2' => $data;
if ($readln) $data = yield;
$data++;
yield 'state3' => $data;
if ($readln) $data = yield;
}
So common way of refactoring in Javascript is a like this (pseudo-code):
use Library; // guess library hadn't written in Promise paradigm at all, only sync...
function controller()
{
$result = Library::call(...someArgs); // devs spotted unexpected error something here
anotherAction($result);
}
async function newController()
{
$result = await Promise.resolve()
.catch('functionThatCatchAndChangesResultToKnownDefault')
.then('anotherAction');
return result;
}
async function newControllerInPhp()
{
try {
$result = Library::call(...someArgs); // devs spotted unexpected error something here
} catch (\Throwable $e) {
// logger
// sentry
// ...tonns of methods that you forget guaranteed
// ...and the problem is in that, so you ALWAYS needed to write try/catch block making your code like scratch with this hell
$result = $resultDefault;
}
anotherAction($result);
return result;
}
function libraryCall(...args)
{
return new Promise((ok) => ok(Library::call(...args));
}
Lets talk?
t.me/gzhegow
The text was updated successfully, but these errors were encountered:
Look the promise power in JS:
Look the code in PHP:
Reasons:
So your library is a Deferred factory without exception catching and without known sugar for js devs.
And can be simplified with:
So common way of refactoring in Javascript is a like this (pseudo-code):
Lets talk?
t.me/gzhegow
The text was updated successfully, but these errors were encountered: