How to make this function return a Boleano type

Asked

Viewed 36 times

0

I deleted the question Variables and Scope Typescript because as a solution I made the validations I needed within the function of the event itself onload then I think the solution changed the structure of the question, however I have a problem, the function always returns true even when it enters the validations it should return false:

isFileValid( file : File ) : boolean {
    let image = new Image();

    image.src = URL.createObjectURL( file );
    image.onload = () : boolean => {
        let imageH = image.height;
        let imageW = image.width;

        if ( imageH != this.maxheight || imageW != this.maxwidth ){

            let notifications: Message = {
                id: Math.random() * 10,
                type: 'alert-danger',
                button: true,
                content: 'a imagem ' + file.name 
                + " precisa ter exatamente as dimensões " 
                + '<strong>${this.maxwidth}</strong>x<strong>${this.maxheight}px!`
            };

            this.notificationsService.add(notifications);
            return false;
        }

        if ( file.size > ( this.maxsize * this.MB ) ) {
            let notifications: Message = {
                id: Math.random() * 10,
                type: 'alert-danger',
                button: true,
                content: 'a imagem' + file.name 
                + `ultrapassa o limite de ${ this.FormateBytes( this.maxsize * this.MB )}`
            };

            this.notificationsService.add(notifications);
            return false;
        }

        return true;
    }

    return true;
}

how can I make for the call if (isFileValid(imagem) ) she returns true or false?

1 answer

1


Within your function, you have the onload that runs asynchronously. That way, you should turn your function into a Promise:

function isFileValid(file: File): Promise<any> {
    let promise = new Promise<any>((resolve, reject) => {
        let image = new Image();

        image.src = URL.createObjectURL(file);
        image.onload = (): void => {
            let imageH = image.height;
            let imageW = image.width;

            if (imageH != this.maxheight || imageW != this.maxwidth) {

                let notifications: Message = {
                    id: Math.random() * 10,
                    type: 'alert-danger',
                    button: true,
                    content: 'a imagem ' + file.name
                        + " precisa ter exatamente as dimensões "
                        + '<strong>${this.maxwidth}</strong>x<strong>${this.maxheight}px!`
                };

                this.notificationsService.add(notifications);
                reject();
            }

            if (file.size > (this.maxsize * this.MB)) {
                let notifications: Message = {
                    id: Math.random() * 10,
                    type: 'alert-danger',
                    button: true,
                    content: 'a imagem' + file.name
                        + `ultrapassa o limite de ${this.FormateBytes(this.maxsize * this.MB)}`
                };

                this.notificationsService.add(notifications);
                reject();
            }

            resolve();
        }
    });


    return promise;
}

Then to check whether it is valid or not:

isFileValid(imagem)
    .then(()=>{
        console.log("Arquivo válido");
    })
    .catch(()=>{
        console.log("Arquivo inválido");
    });
  • Thanks @Allan exactly what I needed!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.