After some discussions and comments, my task was solved by this approach:
const someFunction = timeout => new Promise(resolve => setTimeout(resolve, timeout));
someFunction(timeout).then(() => doSomething()).catch(() => console.log(`This code will not be executed`));
If an exception occurs in an asynchronous function:
someFunction(timeout).then(ErrorEvent).catch(() => console.log(`This code will not be executed`));
Such solutions are still possible:
const testF = () => { throw new Error(`Упс! Бросили исключение.`) }
const genPromise = () => new Promise((resolve, reject) => {
setTimeout(() => {
try {
const res = testF()
resolve(res)
} catch (e) { reject(e) }
}, 1e3)
})
t1: {
const log = console.log.bind(console, 't1:')
const _then = log.bind(console, 'then:')
const _catch = log.bind(console, 'catch:')
genPromise()
.then(_then)
.catch(_catch)
.then(_then)
}
t2: {
const log = console.log.bind(console, 't2:')
const _then = log.bind(console, 'then:')
const _catch = log.bind(console, 'catch:')
void async function () {
let pRes = null
try {
pRes = await genPromise()
} catch (e) {
_catch(e.message)
}
_then(pRes)
}()
}