Commit 5b6946cc authored by Jakob Moser's avatar Jakob Moser
Browse files

Replace ugly timeout with more elegant Promies construct

parent c628b323
Loading
Loading
Loading
Loading
+8 −13
Original line number Diff line number Diff line
@@ -228,10 +228,9 @@ class ExerciseExecutionContext {
     * @returns An object that has the method "hasOutput"
     */
    verify(command) {
        if(command) {
            // TODO await this, and maybe then remove the timout below
            runCommand(command)
        }
        // If a command is provided, store the returned promise (which is resolved as soon as the command was run),
        // if no command is provided, directly start with a resolved promise.
        const commandPromise = command ? runCommand(command) : Promise.resolve()
        const verifyHandler = this._verifyHandler

        return {
@@ -246,9 +245,8 @@ class ExerciseExecutionContext {
             * @param {Function|String|Object} param String or list of strings to exactly match, or predicate function
             */
            hasOutput(param) {
                // Wait for 200ms to ensure that the VM has processed the input and written some
                // output to the terminal
                setTimeout(() => {
                // Wait until the VM has processed the input and written some output to the terminal
                commandPromise.then(() => {
                    const terminalContents = getTerminalContents()
                    const latestEntry = terminalContents[terminalContents.length - 1]

@@ -267,7 +265,7 @@ class ExerciseExecutionContext {
                    const result = !!predicate(latestEntry.output)

                    verifyHandler(result)
                }, 200)
                })
            },

            /**
@@ -280,13 +278,10 @@ class ExerciseExecutionContext {
             * @param {Function|String} param String to exactly match, or predicate function
             */
            commandWas(param) {
                // Wait for 200ms to ensure that the VM has processed the input and written some
                // output to the terminal
                setTimeout(() => {
                commandPromise.then(() => {
                    const terminalContents = getTerminalContents()
                    const latestEntry = terminalContents[terminalContents.length - 1]


                    // Poor man's switch-case: depending on the "typeof param", a different verification predicate is chosen
                    const predicate = {
                        "function": param,
@@ -296,7 +291,7 @@ class ExerciseExecutionContext {
                    const result = !!predicate(latestEntry.input)

                    verifyHandler(result)
                }, 200)
                })
            }
        }
    }