Skip to content

Instantly share code, notes, and snippets.

View enkot's full-sized avatar
👌
Focusing

Taras Batenkov enkot

👌
Focusing
View GitHub Profile
@enkot
enkot / promise.ts
Last active August 18, 2018 14:06
promise.ts
@Component
class LoginPage extends Vue {
// ...
loginUser() {
api.loginUser(this.email, this.password)
.then(handleLogin)
.catch(error => Toast.error(error.message))
}
}
@Component
class LoginPage extends Vue {
// ...
async loginUser() {
try {
const token = await api.loginUser(this.email, this.password)
handleLogin(token)
} catch(error) {
Toast.error(error.message)
}
@Component
class LoginPage extends Vue {
// ...
async loginUser() {
try {
const token = await api.loginUser(this.email, this.password)
handleLogin(token)
} catch(error) {
Toast.error(error.message)
}
// main.js
Vue.config.errorHandler = function (error) {
Toast.error(error.message)
console.warn(error.message)
}
// App.vue
@Component
export default class App extends Vue {
async created() {
@Component
class App extends Vue {
// ...
decodeData(data) {
try {
this.decodedData = atob(data) // can throw DOMException
} catch(error) {
Toast.error(error.message)
}
}
class Main {
public static void main(String[] args) throws IOException {
FileReader file = new FileReader("C:\\test\\a.txt");
// ...
}
}
// register global handler
catchDecorator.register((error) => Toast.error(error.message))
@Component
class LoginPage extends Vue {
// ...
// catch errors and run global handler
@Catch()
async loginUser() {
const token = await api.loginUser(this.email, this.password)
function log(func) {
return function() {
func()
console.log('Function called')
}
}
function getData() { ... }
getData = log(getData)
function Catch(target, key, descriptor) {
const originalMethod = descriptor.value
descriptor.value = async function(...args) {
try {
return await originalMethod.apply(this, args)
} catch (error) {
console.warn(error.message)
Toast.error(error.message)
}
import Catch from './catchDecorator'
@Component
export default class App extends Vue {
@Catch
async created() {
const data = await api.getData() // throws Error
}
@Catch