the/promises

使用 GuzzleHttp 提供了 The\Promise 和 The\PromiseInterface。其他框架可以提供兼容实现。

1.0 2019-04-02 21:25 UTC

This package is auto-updated.

Last update: 2024-08-29 05:00:31 UTC


README

通过 The 命名空间提供对 Promises 实现的访问。

用法

这是 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
});