function stream
stream(input: RequestInfo | URL,init?: RequestInit): Promise<AsyncGenerator<ServerSentEventMessage, void, unknown>>Convenience function that will fetch with the given arguments and, if ok, will return the events async iterator.
If the response is not ok (status 200-299), the Response is thrown.
Examples
Example 1
Example 1
// NOTE: throws `Response` if not 2xx status let events = await stream('https://api.openai.com/...', { method: 'POST', headers: { 'Authorization': 'Bearer <token>', 'Content-Type': 'application/json', }, body: JSON.stringify({ stream: true, // ... }) }); for await (let event of events) { console.log('<<', JSON.parse(event.data)); }
Parameters
input: RequestInfo | URLoptional
init: RequestInitReturn Type
Promise<AsyncGenerator<ServerSentEventMessage, void, unknown>>