- Attempting to fetch the content from a remote.
- Performing a slow MySQL query.
- Network transmission is slow.
- Deferring is needed.
- Something slow needs to be done.
- I/O process waits and does nothing.
- Spawn a thread/process for each request.
- Handle requests asynchronously within one thread.
- Asynchronous, non-blocking I/O for web service, filesystem, and database connection, etc.
- Implementing high concurrency in a single thread. No cost for forking or spawning threads.
- Event watchers
|Watcher Type||Callback Signature|
|defer()||function (string $watcherId, $callbackData)|
|delay()||function (string $watcherId, $callbackData)|
|repeat()||function (string $watcherId, $callbackData)|
|onReadable()||function (string $watcherId, $stream, $callbackData)|
|onWritable()||function (string $watcherId, $stream, $callbackData)|
|onSignal()||function (string $watcherId, $signal, $callbackData)|
- Controlling event watchers
|run()||Start the event loop with all watcher active.|
|stop()||Terminate the event loop and continue execution to the next line after
|enable()||Resume a disabled watcher back to the event loop.|
|disable()||Temporarily remove a watcher from the event loop.|
|reference()||Mark a watcher as referenced.|
|unreference()||Mark a watcher as unreferenced.|
|cancel()||Destroy a watcher.|
- What are Promises?
- Asynchronous functions should return an instance of a class which implements
- Promises are created by an instance of
Amp\Deferred, which resolves the promised value, and throws an exception when an error occurs.
- Promise Combinators (in namespace
Amp\Promise) combine multiple promises to a single Promise.
|all()||Resolve when all Promises in the group resolve.|
|some()||Resolve when no less than one Promise resolves.|
|any()||Resolve even when all Promises fail.|
|first()||Resolve when the first Promise in the group resolves.|
- Promise Helpers (in namespace
|rethrow()||Forward errors generated by the given Promise to the event loop.|
|timeout()||Throw an exception if the given Promise fail to resolve or reject.|
|wait()||Synchronously wait for a Promise to resolve.|
- What are coroutines?
- In Amp, all yields of coroutines must be one of the following type.
|Amp\Promise||Control will be returned to the Coroutine once resolved.|
|React\Promise\PromiseInterface||Will be adapted to
|array||Array of Promises will be combined implicitly to
- Coroutine helpers (in Amp namespace)
|coroutine(callable $callback) : callable||Wrap a function into a coroutine.|
|asyncCoroutine(callable $callback) : callable||Callback function do not return a Promise when called.|
|call(callable $callback, …$args) : Promise||Call the given function, and return a Promise.|
|asyncCall(callable $callback, …$args) : void||Do not return a Promise.|
- In Amp, an iterator iterates through a set of Promises, and resolves alongside with the Promises. It can be recognized as a “special” Promise which can be resolved multiple times.
- Iterators are created by
- Iterator functions are listed below.
|Iterator::getCurrent()||If Promise resolves to
|Iterator::advance()||Return a Promise which indicates whether there’s a value to consume.|
|Emitter::emit()||Emits a new value to the Iterator.|
|Emitter::complete()||Mark an iterator as complete and no further emits will be done.|
|Emitter::iterate()||Return instance of Iterator.|
|Iterator\fromIterable()||Converts arrays or
- Producer is a simplified form of emitter which can be used when all values can be emitted in a single coroutine.
- Iterator combination functions combine an array of Iterators into a single Iterator.
|Iterator\concat()||Iterators are resolved one by one.|
|Iterator\merge()||Iterators are resolved simultaneously.|
- Iterator transformation functions intervene the resolution of Iterators using Producer.
|Iterator\map()||Transform the resolved value into another value.|
|Iterator\filter()||Resolved value is omitted if filter callback returns false.|
- Amp provides cancellation of a specific asynchronous operation. but it does not and cannot automatically handle cancellation. Instead, you should handle cancellation manually after its request.
- Cancellation is implemented using
|CancellationTokenSource::getToken()||Returns a unique CancellationToken instance.|
|CancellationTokenSource::cancel()||Emits a Cancellation request to its CancellationToken.|
|CancellationToken::isRequested()||Resurns whether there is a Cancellation request.|
|CancellationToken::throwIfRequested()||Throws CancelledException if Cancellation request exists.|
|CancellationToken::subscribe()||Callback will be invoked when the request occurs.|
|CancellationToken::unsubscribe()||Disable a specified callback by id.|