首页 > 解决方案 > JS 使用 onSuccess() 语法

问题描述

我有一些代码,我只是看到语法有问题。

这是代码:

async mymethod(onSuccess, onFailure) {
    try {
        // Do some here
        onSuccess()
    }
    catch (e) {
        //this was an error
    }
}

我想做的就是onSuccess()我想做的事情。

试过:

onSuccess((function() {
    // Do something
}))

但似乎存在语法错误。

我如何使用onSuccess()它并用它做些什么?

标签: javascripttypescript

解决方案


您缺少function关键字:

async function mymethod(onSuccess, onFailure) {
    try {
        // Do some here
        onSuccess()
    }
    catch (e) {
        //this was an error
        onFailure()
    }
}

mymethod(() => alert('success'), () => alert('failure'));

推荐阅读