async-call-rpc

A lightweight JSON RPC client & server

Home > async-call-rpc > AsyncGeneratorCall

AsyncGeneratorCall() function

The async generator version of the AsyncCall

Signature:

export declare function AsyncGeneratorCall<OtherSideImplementedFunctions = {}>(thisSideImplementation: null | undefined | object | Promise<object>, options: AsyncCallOptions): _AsyncGeneratorVersionOf<OtherSideImplementedFunctions>;

Parameters

Parameter Type Description
thisSideImplementation null | undefined | object | Promise<object> The implementation when this AsyncCall acts as a JSON RPC server.
options AsyncCallOptions AsyncCallOptions

Returns:

_AsyncGeneratorVersionOf<OtherSideImplementedFunctions>

Remarks

Warning: Due to technical limitation, AsyncGeneratorCall will leak memory. Use it at your own risk.

To use AsyncGeneratorCall, the server and the client MUST support the following JSON RPC internal methods which is pre ECMAScript async generator semantics:

Example

const server = {
     async *generator() {
         let last = 0
         while (true) yield last++
     },
}
type Server = typeof server
const serverRPC = AsyncGeneratorCall<Server>({}, { channel })
async function main() {
     for await (const x of serverRPC.generator()) {
         console.log('Server yielded number', x)
     }
}