February 2019
Beginner
694 pages
18h 4m
English
Our promise objects generally define both a success condition as well as an error condition when calling asynchronous functions. In order to trap these error conditions when using async await syntax, we can use a try...catch block. To illustrate this, let's define a promise that returns an error, as well as an error message, as follows:
function awaitError() : Promise<string> {
return new Promise<string> (
( resolve: (message: string) => void,
reject: (error: string) => void ) =>
{
function afterWait() {
console.log(`calling reject`);
reject("an error occurred");
}
setTimeout(afterWait, 1000);
}
);
}
Here, we have a function named awaitError that is defining and returning a Promise<string>, and is using our standard promise ...
Read now
Unlock full access