Reactjs “我该如何解决?”;应在箭头函数末尾返回一个值;?
我正在使用Reactjs “我该如何解决?”;应在箭头函数末尾返回一个值;?,reactjs,typescript,ecmascript-5,Reactjs,Typescript,Ecmascript 5,我正在使用typescript和eslint。Eslint抱怨在=>箭头之后返回,当我添加它时,这也不起作用-返回新承诺((解决,拒绝)=>返回{}。正确的语法是什么- function getSizeFromObjectUrl(dataURL: string): Promise<any> { return new Promise((resolve, reject) => { try { const img = new Image
typescript
和eslint
。Eslint抱怨在=>
箭头之后返回,当我添加它时,这也不起作用-返回新承诺((解决,拒绝)=>返回{}
。正确的语法是什么-
function getSizeFromObjectUrl(dataURL: string): Promise<any> {
return new Promise((resolve, reject) => {
try {
const img = new Image();
img.onload = () => {
const ratio = Math.min(300.0 / img.width, 300.0 / img.height);
return resolve({
height: img.height * ratio,
width: img.width * ratio
});
};
img.src = dataURL;
} catch (exception) {
return reject(exception);
}
});
}
函数getSizeFromObjectUrl(dataURL:string):承诺{
返回新承诺((解决、拒绝)=>{
试一试{
const img=新图像();
img.onload=()=>{
常数比=数学最小值(300.0/img.宽度,300.0/img.高度);
返回解析({
高度:img.高度*比率,
宽度:img.宽度*比率
});
};
img.src=数据URL;
}捕获(例外){
退货拒绝(例外);
}
});
}
像这样使用它-
const size=wait GetSizeFromoObject(imageUrl);
正确的语法是:
function getSizeFromObjectUrl(dataURL: string): Promise<any> {
return new Promise((resolve, reject) => {
try {
const img = new Image();
img.onload = () => {
const ratio = Math.min(300.0 / img.width, 300.0 / img.height);
resolve({
height: img.height * ratio,
width: img.width * ratio
});
};
img.src = dataURL;
} catch (exception) {
reject(exception);
}
});
}
而不是:
case x:
// your code
正确的语法是:
function getSizeFromObjectUrl(dataURL: string): Promise<any> {
return new Promise((resolve, reject) => {
try {
const img = new Image();
img.onload = () => {
const ratio = Math.min(300.0 / img.width, 300.0 / img.height);
resolve({
height: img.height * ratio,
width: img.width * ratio
});
};
img.src = dataURL;
} catch (exception) {
reject(exception);
}
});
}
而不是:
case x:
// your code
我在承诺解决/拒绝之前删除了退货。这很有效-
function getSizeFromObjectUrl(dataURL: string): Promise<any> {
return new Promise((resolve, reject) => {
try {
const img = new Image();
img.onload = () => {
const ratio = Math.min(300.0 / img.width, 300.0 / img.height);
resolve({
height: img.height * ratio,
width: img.width * ratio
});
};
img.src = dataURL;
} catch (exception) {
reject(exception);
}
});
}
函数getSizeFromObjectUrl(dataURL:string):承诺{
返回新承诺((解决、拒绝)=>{
试一试{
const img=新图像();
img.onload=()=>{
常数比=数学最小值(300.0/img.宽度,300.0/img.高度);
决心({
高度:img.高度*比率,
宽度:img.宽度*比率
});
};
img.src=数据URL;
}捕获(例外){
拒绝(例外);
}
});
}
我在承诺解决/拒绝之前删除了退货。这很有效-
function getSizeFromObjectUrl(dataURL: string): Promise<any> {
return new Promise((resolve, reject) => {
try {
const img = new Image();
img.onload = () => {
const ratio = Math.min(300.0 / img.width, 300.0 / img.height);
resolve({
height: img.height * ratio,
width: img.width * ratio
});
};
img.src = dataURL;
} catch (exception) {
reject(exception);
}
});
}
函数getSizeFromObjectUrl(dataURL:string):承诺{
返回新承诺((解决、拒绝)=>{
试一试{
const img=新图像();
img.onload=()=>{
常数比=数学最小值(300.0/img.宽度,300.0/img.高度);
决心({
高度:img.高度*比率,
宽度:img.宽度*比率
});
};
img.src=数据URL;
}捕获(例外){
拒绝(例外);
}
});
}
规则是关于一致性回报: JavaScript的一个令人困惑的方面是,如果以下任何一项为真,则函数返回undefined:
- 它在退出之前不执行return语句
- 它执行不显式指定值的return
- 它执行未定义的返回
- 它执行返回void,后跟表达式(例如,函数调用)
- 它执行return,后跟计算结果为undefined的任何其他表达式
- 显式返回
块中的内容(try
如果愿意)returnundefined
- 在
块中停止返回catch
- 禁用该函数的规则
- 它在退出之前不执行return语句
- 它执行不显式指定值的return
- 它执行未定义的返回
- 它执行返回void,后跟表达式(例如,函数调用)
- 它执行return,后跟计算结果为undefined的任何其他表达式
- 显式返回
块中的内容(try
如果愿意)returnundefined
- 在
块中停止返回catch
- 禁用该函数的规则