javascript中回调函数的使用
我想用javascript从一个类调用另一个类的函数javascript中回调函数的使用,javascript,Javascript,我想用javascript从一个类调用另一个类的函数 此函数将另一个函数作为参数。 我收到一个语法错误,你知道它在哪里吗? 以下是我收到的错误: /src/containers/NewNote.js 第53行:分析错误:意外标记,应为“,” 51 |试试看{ 52 | const attachment=this.file 53 |?等待s3Upload(this.file、this.inputValue、onUpdate()=>{ |
此函数将另一个函数作为参数。
我收到一个语法错误,你知道它在哪里吗?
以下是我收到的错误:
/src/containers/NewNote.js 第53行:分析错误:意外标记,应为“,” 51 |试试看{ 52 | const attachment=this.file 53 |?等待s3Upload(this.file、this.inputValue、onUpdate()=>{ | ^ 54 | this.setState({progress:progress}); 55 | } 56 |) 我要调用的函数如下所示:
export async function s3Upload(file, header, onUpdate) {
filename = 'toto';
const stored = await Storage.vault.put(filename, file, {
progressCallback(progress) {
var percentage = (progress.loaded / progress.total * 100).toFixed(0);
console.log(`Uploaded: ${percentage}%`, onUpdate);
if (typeof onUpdate === "function") {
try {
onUpdate(percentage);
} catch (e) {
console.error("Error while computing update", e);
}
}
},
contentType: file.type
});
return stored.key;
}
try {
const attachment = this.file ?
await s3Upload(this.file, this.inputValue, onUpdate() => {
this.setState({
progress: progress
});
}) :
null;
await this.createNote({
attachment,
content: this.state.content
});
window.location.href = "/"
} catch (e) {
alert(e);
this.setState({
isLoading: false
});
}
调用函数如下:
export async function s3Upload(file, header, onUpdate) {
filename = 'toto';
const stored = await Storage.vault.put(filename, file, {
progressCallback(progress) {
var percentage = (progress.loaded / progress.total * 100).toFixed(0);
console.log(`Uploaded: ${percentage}%`, onUpdate);
if (typeof onUpdate === "function") {
try {
onUpdate(percentage);
} catch (e) {
console.error("Error while computing update", e);
}
}
},
contentType: file.type
});
return stored.key;
}
try {
const attachment = this.file ?
await s3Upload(this.file, this.inputValue, onUpdate() => {
this.setState({
progress: progress
});
}) :
null;
await this.createNote({
attachment,
content: this.state.content
});
window.location.href = "/"
} catch (e) {
alert(e);
this.setState({
isLoading: false
});
}
及
我看到的语法错误如下:
onUpdate() => {
this.setState({
progress: progress
});
}
应该是:
(progress) => {
this.setState({
progress: progress
});
}
原因是既没有命名的参数,也没有命名的参数,这意味着在本例中无法命名箭头函数。此外,在本例中,当您在该函数的代码中使用arrow函数时,您必须向该函数添加一个参数
progress
,并将其传递给回调函数。问题在哪里?我已更新了问题谢谢,此函数调用是否与s3Upload()中对onUpdate的调用相匹配函数?@cocool97您必须指定一个参数(progress)=>…
。检查我的更新答案。