【问题标题】:executeAsync not passing return value to callbackexecuteAsync 未将返回值传递给回调
【发布时间】:2016-05-20 18:57:42
【问题描述】:

我正在使用实习生 JS/leadfood 测试框架。我正在使用 executeAsync。我希望将 executeAsync 的返回值传递给 executeAsync 的回调,但这并没有发生。以下是否可行?

return  this.remote.get(require.toUrl(url));
    //do stuff
    .executeAsync(function (done) {

        require([<library>],
            function ([<function>]) {
                return <function which returns Promise>
                .then(function (value) {
                    return <function which returns Promise>
                ...
                }).then(function () {
                    done(window.location);

                })
            })

    })
    .then(function (loc) {
        console.log(loc);
    })

执行成功到达 executeAsync 中的最后一个回调。成功调用 executeAsync 的回调。但是传递给 executeAsync 回调的值是undefined。

编辑: 我发现即使您设置了一个非常大的 executeAsync 超时,如果您不调用 this.async(timeout) 指定正确的超时(在撰写本文时默认为 30 秒),该超时也会被忽略。所以问题是测试花费的时间超过 30 秒,并且传递给 done 的值没有进入 executeAsync 的回调。

【问题讨论】:

    标签: javascript intern leadfoot


    【解决方案1】:

    executeAsync 使用回调来确定其函数何时完成运行。此回调会自动作为最后一个参数(如果您不传递任何其他参数,则为唯一参数)传递给 executeAsync 函数:

    define([
        'require',
        'intern!object'
    ], function (
        require,
        registerSuite
    ) {
        registerSuite({
            name: 'test',
    
            foo: function () {
                return this.remote.get(require.toUrl('./index.html'))
                    .setExecuteAsyncTimeout(5000)
                    .executeAsync(function (done) {
                        var promise = new Promise(function (resolve) {
                            setTimeout(function () {
                                resolve();
                            }, 1000);
                        });
    
                        promise.then(function () {
                            return new Promise(function (resolve) {
                                setTimeout(function () {
                                    resolve();
                                }, 1000);
                            });
                        }).then(function () {
                            done(window.location);
                        });
                    })
                    .then(function (loc) {
                        // This prints out a full location object from the
                        // browser.
                        console.log(loc);
                    }); 
            }
        });
    });
    

    【讨论】:

    • 我实现了它,因为你在这里有代码,但 value 对我来说仍然是 undefined。 done 是什么?
    • 正如我所说,executeAsync 函数会自动传递一个回调作为其最后一个参数。这个回调,你可以调用任何你想要的(我通常称之为'done'),应该在你的executeAsync函数完成它正在做的任何事情时调用它。你不能从executeAsync 函数返回一个承诺(嗯,你可以,但实习生不会注意它)。 executeAsync 函数将在浏览器中运行,其中 promise 可能不可用,因此 Intern 使用回调而不是 promise 来获取其返回值。
    • 所以在将一个名为done 的函数传递给传递给executeAsync 的script 参数之后,然后调用done 作为executeAsync 中的最后一步以表示它已经完成执行时,我希望传递给executeAsync(在本例中为value)的回调的值是传递给done 的值。但是value 仍然是undefined。
    • 我更新了我的示例,使其更加完整。此代码按原样工作(假设您有一个与测试文件位于同一位置的 index.html 页面)。
    • 在这种情况下,使用 setTimeout 或使用 Promise 之间没有有意义的区别。如果使用值调用 done 方法,executeAsync 将使用该值解析(只要它是可以序列化的东西)。我已经更新了我的示例以使用 Promises 而不仅仅是一个简单的 setTimeout,而且效果很好。您的测试超时的事实表明您的 Promise 链中的 then 方法没有被调用,这表明链上的某些东西引发了错误。也可以尝试在 catch 块中调用 done,只是为了检查。
    【解决方案2】:

    根据此处的 Leadfoot 文档

    https://theintern.github.io/leadfoot/module-leadfoot_Command.html#executeAsync

    返回

    远程代码返回的值。只能返回可以序列化为 JSON 的值以及 DOM 元素。

    你从执行的函数中返回了什么?

    【讨论】:

    • 我正在返回一个数组,所以这应该不是问题。
    • @cosmos1990 你确定吗?看起来您实际上可能正在返回一个承诺,该承诺最终会解析为一个数组。绝对不是一回事。
    • 可能是这样。无论如何,我现在传入一个回调函数并调用它,而不是像@jason0x43 所建议的那样返回一个值。
    猜你喜欢
    • 2017-05-06
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    相关资源
    最近更新 更多