[英]How can i call an async function inside another function in a javascript class?
i am trying to call an async function inside a class but keep getting an error that this.getcategory.then is not a function at category.js:21
I have a form which is used by the admin to add more categories into the database.我正在尝试在类中调用异步函数,但不断收到错误,提示
this.getcategory.then is not a function at category.js:21
我有一个表单,管理员使用该表单将更多类别添加到数据库中。 categform.addEventListener('submit', e => {
this line will listen for the submit event after which the data is taken and inserted into the the database using the code using the code snippet below categform.addEventListener('submit', e => {
此行将侦听提交事件,然后使用以下代码片段获取数据并将其插入到数据库中
super.postText("../includes/getcategoy.inc.php", categformData)
.then(res => {
console.log(res);
// 1. if the results are ok fetch the fresh data from the database
this.getcategory.then(
res => {
console.log(res);
}
)
}).catch((error) => {
console.error('Error:', error);
});
now the above code returns a promise then if the result is ok i call another function in the same class got get the latest data form the database.现在上面的代码返回一个承诺,然后如果结果正常,我调用同一个类中的另一个函数从数据库中获取最新数据。 the fuction is
this.getcategory
remember i am calling the function inside the then and am getting an error.功能是
this.getcategory
记住我在then内部调用函数并且出现错误。 the reason why i am calling inside the then is because only want it to be executed after sending of the data into the database has been resolved.我在then内部调用的原因是因为只希望在将数据发送到数据库后才执行它已解决。
but when i call it outside the first function i do not get an error.. if you look just after the catch block i have commented it out.但是当我在第一个函数之外调用它时,我没有收到错误..如果您只查看catch 块,我已将其注释掉。 if i call it there i do not get an error.
如果我在那里调用它,我不会收到错误消息。 yet i do not want to call it there.
但我不想在那里打电话。 remember the function returns a promise and it is defined as the last function in the class
记住该函数返回一个promise,它被定义为类中的最后一个函数
how can i solve this problem, below is the whole code我该如何解决这个问题,下面是整个代码
import { FETCH } from "./fetch.js";
class CATEGORY extends FETCH {
constructor() {
super()
}
listencategory() {
//1. Listen for the button click
const categform = document.getElementById('categotyform');
categform.addEventListener('submit', e => {
e.preventDefault();
//2. get the data from form
const categformData = new FormData(categform);
super.postText("../includes/getcategoy.inc.php", categformData)
.then(res => {
// console.log(res);
// 1. if the results are ok fetch the fresh data from the database
this.getcategory.then(
res => {
console.log(res);
}
)
}).catch((error) => {
console.error('Error:', error);
});
});
//this.getcategory().then(res=>{
// console.log(res);
//
//})
}
async getcategory() {
try {
const results = await super.get("../includes/getcategoy.inc.php");
return results;
} catch (error) {
console.log(error);
}
}
}
const categ = new CATEGORY;
categ.listencategory();
i am trying to get data that has been returned by async getcategory()我正在尝试获取async getcategory()返回的数据
getcategory is an Async
function that should be made a function call like below. getcategory 是一个
Async
函数,应该像下面这样进行函数调用。
this.getcategory().then(
res => {
console.log(res);
}
)
This link helps you create and use promises effectively.此链接可帮助您有效地创建和使用承诺。 Async functions - making promises friendly
异步函数 - 使承诺友好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.