the/promises

Provides The\Promise and The\PromiseInterface using GuzzleHttp. Other frameworks can provide compatible implementations.

1.0 2019-04-02 21:25 UTC

This package is auto-updated.

Last update: 2024-11-29 05:56:55 UTC


README

Provides access to the Promises implementation through the The namespace.

Usage

This is an async implementation of fread.

function async_fread($handle, $length) {
	return new \The\Promise(function($yes, $no) use($handle, $length) {
		\The\Loop::onReadable($file, function() use($yes) {
			$yes(fread($handle, $length));
		});
	});
}

// Using it synchronously:

$data = async_fread($handle, $length)->wait();
// Code continues here. The script simply pauses until the data is available.

// Using it asynchronously in amphp and similar frameworks:

$data = yield async_fread($handle, $length);
// Code continues here, but other stuff can be happening while waiting because of yield

// Using it asynchronously in other frameworks:

async_fread($handle, $length)->then(function($data) {
	// Code continues here
});