> Thomas Bley <[email protected]> hat am 22.11.2025 12:42 CET geschrieben:
> 
>  
> > Edmond Dantes <[email protected]> hat am 22.11.2025 11:37 CET geschrieben:
> > 
> >  
> > Hello
> > 
> > > I expect that's already a good improvement for many use cases to read 
> > > multiple files in parallel, run multiple queries in parallel, etc.
> > 
> > I see. You want to get several promises to wait for?
> > Then there is a good way to do it without additional functions:
> > 
> > ```php
> > $promise1 = spawn(file_get_content(...), "file1.txt");
> > $promise2 = spawn(file_get_content(...), "file2.txt");
> > $promise3 = spawn(file_get_content(...), "file3.txt");
> > ```
> > 
> > This is equivalent because a coroutine is a Future. In the same way,
> > you can turn any other function into a Promise without creating a
> > separate API.
> > 
> > There is a nuance regarding resources. And in certain scenarios,
> > creating an array of Promises in another way can save memory. For
> > example, a scenario where you need to handle a large array of sockets.
> > Such cases require a special API.
> > 
> > ---
> > Ed
> 
> Hello,
> 
> function return types should not depend on the outside context (spawn, hook, 
> ini, etc.) because when the code gets more complex, it's very hard to find 
> the outside context.
> 
> So file_get_contents() should always return string|false, 
> file_get_contents_async() should always return a promise object.
> 
> From the example I would expect it like this:
> 
> $promise1 = file_get_content_async("file1.txt");
> $promise2 = file_get_content_async("file2.txt");
> $promise3 = file_get_content_async("file3.txt");
> 
> // do sth else
> 
> $content1 = $promise->await();
> $content2 = $promise->await();
> $content3 = $promise->await();
> 
> PHP got very successful by making things easier than in C, this should be the 
> path to continue.
> 
> Best Regards
> Thomas

I'm sorry, the correct example should be:

$promise1 = file_get_content_async("file1.txt");
$promise2 = file_get_content_async("file2.txt");
$promise3 = file_get_content_async("file3.txt");

// do sth else

$content1 = $promise1->await();
$content2 = $promise2->await();
$content3 = $promise3->await();

Best Regards
Thomas

Reply via email to