Created
March 2, 2024 06:39
-
-
Save azu/8eaf5f74c9409a94f8ff98d090d33c9a to your computer and use it in GitHub Desktop.
Async Generator usecase: https://vercel.com/blog/ai-sdk-3-generative-ui
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const draw = (output) => console.log(output); | |
const getWeather = async (city) => { | |
await new Promise((resolve) => setTimeout(resolve, 1000)); | |
return "sunny"; | |
}; | |
const render = async function* ({ city }) { | |
yield `<Spinner />`; | |
const weather = await getWeather(city); | |
return `<Weather info=${weather} />`; | |
}; | |
// Please draw the weather after using this `render`. | |
async function main() { | |
const gen = render({ city: "New York" }); | |
while (true) { | |
const { value, done } = await gen.next(); | |
draw(value); | |
if (done) { | |
break; | |
} | |
} | |
} | |
await main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment