Run ❯
Get your
own Node
server
❯
Run Code
Ctrl+Alt+R
Change Orientation
Ctrl+Alt+O
Change Theme
Ctrl+Alt+D
Go to Spaces
Ctrl+Alt+P
// Create a typed Promise that resolves to a string const fetchGreeting = (): Promise
=> { return new Promise((resolve, reject) => { setTimeout(() => { const success = Math.random() > 0.5; if (success) { resolve("Hello, TypeScript!"); } else { reject(new Error("Failed to fetch greeting")); } }, 1000); }); }; // Using the Promise with proper type inference fetchGreeting() .then((greeting) => { // TypeScript knows 'greeting' is a string console.log(greeting.toUpperCase()); }) .catch((error: Error) => { console.error("Error:", error.message); });
Expected console output:
HELLO, TYPESCRIPT!