How to make this function return a Boolean type

0

I deleted the question Variables and Scope TypeScript because as a solution I did the validations I needed within the function of the onload itself event I think the solution changed the question structure, but I'm having a problem, the function always returns true even when it enters the validations that 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 do to call if (isFileValid(imagem) ) to return true or false ?

    
asked by anonymous 04.08.2018 / 18:44

1 answer

1

Within your role, you have the onload that runs asynchronously. That way you should turn your role 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 check to see if it is valid:

isFileValid(imagem)
    .then(()=>{
        console.log("Arquivo válido");
    })
    .catch(()=>{
        console.log("Arquivo inválido");
    });
    
04.08.2018 / 22:22