Option 3
would be the right answer because errors are caught by the catch block only when there are part of the promise chain. In the current code snippet, error thrown at line no. 2 is outside the promise chain as the chain is initiated at line no 3. If it would have been like --
function first() {
return Promise.resolve(1);
}
function init() {
return first()
.then(v => {
throw new Error("I am an error");
})
.catch((err) => console.log("Error caught -- ", err));
}
init();
Then output would be "Error caught -- I am an error" because we throwing an error from a function which is part of the promise chain.
For useful and amazing frontend and programming tutorials: https://bit.ly/devtools-yt