Suspected error in Item 25: Use async Functions Instead of Callbacks for Asynchronous Code
liby opened this issue · comments
You're seeing consistent behavior because your implementation of fetchURL
is synchronous. If you make it async (but without declaring it async
):
function fetchURL(url: string, cb: (response: string) => void) {
setTimeout(() => {
cb(url);
}, 10);
}
Then you can call getUser("test")
twice and observe different behaviors.
Here's an updated version of your playground link.