Skip to content

Promise usage results in errors #293

@shkup

Description

@shkup

I use grunt with grunt-contrib-jshint on my bundle process for my asp.net core typescript files.
I have this super simple typescript code:

function delay(delay: number) {
    return new Promise(r => {
        setTimeout(r, delay);
    })
}

class Timer {
    constructor(public counter: number = 3, public delayMs: number = 1000) {
        this.doTimer();
    }

    async doTimer() {
        for (let i = 0; i < this.counter; i++) {
            await delay(this.delayMs);
            this.counter = this.counter - 1;
            console.log(this.counter);
        }
    }
}

The result js looks like this:

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
function delay(delay) {
    return new Promise(r => {
        setTimeout(r, delay);
    });
}
class Timer {
    constructor(counter = 3, delayMs = 1000) {
        this.counter = counter;
        this.delayMs = delayMs;
        this.doTimer();
    }
    doTimer() {
        return __awaiter(this, void 0, void 0, function* () {
            for (let i = 0; i < this.counter; i++) {
                yield delay(this.delayMs);
                this.counter = this.counter - 1;
                console.log(this.counter);
            }
        });
    }
}
//# sourceMappingURL=Timer.js.map

grunt-contrib-jshint is configured like that in my Gruntfile.js:

module.exports = function (grunt) {
    grunt.initConfig({
         concat: {
            all: {
                src: ['Script/Timer.js'],
                dest: 'temp/app.js'
            }
        },
        jshint: {
            files: ['temp/app.js'],
            options: {
                '-W069': false,
                'esversion': 6,
            }
        },
      }
    });

     grunt.loadNpmTasks('grunt-contrib-concat');
     grunt.loadNpmTasks('grunt-contrib-jshint');
};

grunt-contrib-jshint outputs the following errors:

temp/app.js
      3 |    return new (P || (P = Promise))(function (resolve, reject) {
                                           ^ Bad constructor.
      6 |        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
                                                                                                                           ^ Expected an 

I also tried to configure jshint with esversion 9. grunt-contrib-jshintis version 2.1.0.
What is wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions