score:2

Accepted answer

the rule is about consistent return: https://eslint.org/docs/rules/consistent-return

a confusing aspect of javascript is that a function returns undefined if any of the following are true:

  • it does not execute a return statement before it exits
  • it executes return which does not specify a value explicitly
  • it executes return undefined
  • it executes return void followed by an expression (for example, a function call)
  • it executes return followed by any other expression which evaluates to undefined

if any code paths in a function return a value explicitly but some code path do not return a value explicitly, it might be a typing mistake, especially in a large function.

so what you need you can do to get rid of the message is:

  • explicitly return something in the try block (return undefined if you will)
  • stop returning in the catch block
  • disable the rule for that function

score:0

the correct syntax is:

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);
        }
    });
}

it's actually complaining about the return before resolve / reject not after the arrow. cuz the resolve and reject functions are voids

for the error unexpected lexical declaration in case block., use the case like this :

case x: {
    // your code goes here
}

instead of:

case x: 
    // your code

score:0

i removed return before promise resolve/reject. this works -

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);
        }
    });
}

Related Query

More Query from same tag