Skip to content

Instantly share code, notes, and snippets.

@rdlowrey
Last active August 29, 2015 14:17
Show Gist options
  • Save rdlowrey/27c5f848e2ccaf43b438 to your computer and use it in GitHub Desktop.
Save rdlowrey/27c5f848e2ccaf43b438 to your computer and use it in GitHub Desktop.

Revisions

  1. rdlowrey revised this gist Mar 17, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion yield-from-example.php
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ function myHttpHandler(Request $request, Response $response) {

    if ($session->hasValue('isLoggedIn')) {
    // pass the individual promises from generateHttpBody() through using `yield from`
    // when generateBody() finishes we get its return result in $body
    // when generateHttpBody() finishes we get its return result in $body
    $rawHtmlBody = yield from generateHttpBody($request);
    } else {
    $response->setStatus(301);
  2. rdlowrey revised this gist Mar 17, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion yield-from-example.php
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ function myHttpHandler(Request $request, Response $response) {
    $session = yield loadSessionFromRequest($request);

    if ($session->hasValue('isLoggedIn')) {
    // pass the individual promises from generateBody() through using `yield from`
    // pass the individual promises from generateHttpBody() through using `yield from`
    // when generateBody() finishes we get its return result in $body
    $rawHtmlBody = yield from generateHttpBody($request);
    } else {
  3. rdlowrey revised this gist Mar 17, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions yield-from-example.php
    Original file line number Diff line number Diff line change
    @@ -9,14 +9,14 @@ function myHttpHandler(Request $request, Response $response) {
    if ($session->hasValue('isLoggedIn')) {
    // pass the individual promises from generateBody() through using `yield from`
    // when generateBody() finishes we get its return result in $body
    $body = yield from generateHttpBody($request);
    $rawHtmlBody = yield from generateHttpBody($request);
    } else {
    $response->setStatus(301);
    $response->setHeader("Location", "http://domain.com/login");
    $body = '';
    $rawHtmlBody = '';
    }

    $response->send($body);
    $response->send($rawHtmlBody);
    }

    function generateHttpBody(Request $request, Session $session) {
  4. rdlowrey created this gist Mar 17, 2015.
    39 changes: 39 additions & 0 deletions yield-from-example.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    <?php

    function myHttpHandler(Request $request, Response $response) {
    // async function that returns a promise
    // we use yield to wait for that promise to resolve then resume here
    // if there's some kind of error it will be thrown into our generator
    $session = yield loadSessionFromRequest($request);

    if ($session->hasValue('isLoggedIn')) {
    // pass the individual promises from generateBody() through using `yield from`
    // when generateBody() finishes we get its return result in $body
    $body = yield from generateHttpBody($request);
    } else {
    $response->setStatus(301);
    $response->setHeader("Location", "http://domain.com/login");
    $body = '';
    }

    $response->send($body);
    }

    function generateHttpBody(Request $request, Session $session) {
    // returns a promise; pause here until the promise resolves.
    // this promise is passed through transparently inside myHttpHandler()
    // because we used `yield from` there.
    $user = yield loadUserFromDatabase($session->getValue('userId'));

    // Another promise -- again, this is passed through transparently
    // because we used `yield from` in myHttpHandler()
    $template = yield loadTemplateFromRedis($request->getUriPath());

    // ... do some stuff here to set template values ... //

    return $rawHtmlBody;
    }

    // And above this layer in the HTTP server code:
    // $promise = coroutine(myHttpHandler($request, $response));
    // $promise->when(...); // we know the response completed here