我有一些代码,我只是发现语法有问题。这是代码:async mymethod(onSuccess, onFailure) { try { // Do some here onSuccess() } catch (e) { //this was an error }}我想做的是onSuccess()我想做的事情。尝试:onSuccess((function() { // Do something}))但似乎存在语法错误。我如何使用它onSuccess()并用它做一些事情?
2 回答
皈依舞
TA贡献1851条经验 获得超3个赞
您缺少function关键字:
async function mymethod(onSuccess, onFailure) {
try {
// Do some here
onSuccess()
}
catch (e) {
//this was an error
onFailure()
}
}
mymethod(() => alert('success'), () => alert('failure'));
www说
TA贡献1775条经验 获得超8个赞
由于您使用的是打字稿和 a,因此class您需要包含public访问修饰符,因为方法是private默认的。
class Foo {
public async mymethod(onSuccess, onFailure) {
try {
// Do some here
onSuccess()
}
catch (e) {
//this was an error
onFailure()
}
}
}
const foo = new Foo();
foo.mymethod(() => { console.log('success') }, () => { console.log('failure') });
添加回答
举报
0/150
提交
取消