TypeScript already does this
// users is a Promise<T>
const users = getUsers(); // <<< missing await
// users.length is not a property of users... then and catch are
console.log(users.length);
You may find situations where you are not told about your mistake - where the types are compatible, for example, I missed the wait here:
function delay(ms: number) {
return new Promise<number>(function(resolve) {
setTimeout(() => {
resolve(5);
}, ms);
});
}
async function asyncAwait() {
let x = await delay(1000);
console.log(x);
let y = delay(1000);
console.log(y);
return 'Done';
}
asyncAwait().then((result) => console.log(result));
console.log
promises, , .
... , .
let y: number = delay(1000);