Skip to content

Instantly share code, notes, and snippets.

@janit
Last active February 21, 2016 07:18
Show Gist options
  • Save janit/1e797df814eef509fcff to your computer and use it in GitHub Desktop.
Save janit/1e797df814eef509fcff to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Icicle\Coroutine\Coroutine;
use Icicle\Loop;
use Icicle\Awaitable;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$messages = array();
$routine1 = new Coroutine($this->sayHello('Baby'));
$routine2 = new Coroutine($this->sayHello('Ginger'));
$routine3 = new Coroutine($this->sayHello('Posh'));
$routine4 = new Coroutine($this->sayHello('Scary'));
$routine5 = new Coroutine($this->sayHello('Sporty'));
$messages[] = $routine1->wait();
$messages[] = $routine2->wait();
$messages[] = $routine3->wait();
$messages[] = $routine4->wait();
$messages[] = $routine5->wait();
Loop\Run();
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
'messages' => $messages
]);
}
public function sayHello($name){
$delay = rand(1,5);
$message = array("Hello! My name is " . $name, $delay);
$promise = Awaitable\resolve($message);
yield $promise->delay($delay);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment