javascriptwebcamwebcam.js

What is the error in .catch inside webcam.js file


In an application I am using webcam. TO access it I used webcam.js (https://pixlcore.com/). But when I opened it in eclipse it shows error as:Syntax error on token "catch", Identifier expected. little code snippet:

            var self = this;
            this.mediaDevices.getUserMedia({
                "audio": false,
                "video": this.params.constraints || {
                    mandatory: {
                        minWidth: this.params.dest_width,
                        minHeight: this.params.dest_height
                    }
                }
            })
            .then( function(stream) {
                // got access, attach stream to video
                video.src = window.URL.createObjectURL( stream ) || stream;
                self.stream = stream;
                self.loaded = true;
                self.live = true;
                self.dispatch('load');
                self.dispatch('live');
                self.flip();
            })
            .catch( function(err) {  //here shows error
                return self.dispatch('error', "Could not access webcam: " + err.name + ": " + err.message, err);
            });

What is the reason and how to resolve it?


Solution

  • The problem is apparently that catch is a reserved keyword, and thus your code-checker thinks this is an error. However, your code checker is actually mistaken, and catch is also a valid method call. That is, unless you are an older version if IE.

    In older versions of IE, this code will fail because it also had this issue where it assumed a catch outside of a try/catch was invalid. I believe this issue was fixed in either IE9 or IE10, not sure.

    Anyhow, you can work around this issue for both old IE and other things with this general issue by using catch in a string with bracket property access:

    // ...
    .then( function(stream) {
        // ...
    })
    ['catch']( function(err) { 
        // ...
    });