Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript executeAsync未将返回值传递给回调_Javascript_Intern_Leadfoot - Fatal编程技术网

Javascript executeAsync未将返回值传递给回调

Javascript executeAsync未将返回值传递给回调,javascript,intern,leadfoot,Javascript,Intern,Leadfoot,我正在使用InternJS/leadfood测试框架。我正在使用executeAsync。我希望executeAsync的返回值被传递到executeAsync的回调,但这并没有发生。是否应该进行以下工作 return this.remote.get(require.toUrl(url)); //do stuff .executeAsync(function (done) { require([<library>], fun

我正在使用InternJS/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);
    })
返回this.remote.get(require.toUrl(url));
//做事
.executeAsync(函数(完成){
需要([],
函数([])){
返回
.然后(函数(值){
返回
...
}).然后(函数(){
完成(窗口位置);
})
})
})
.然后(功能(loc){
控制台日志(loc);
})
执行成功到达executeAsync中的最后一个回调。已成功调用对executeAsync的回调。但是传递给executeAsync回调的值是未定义的

编辑:
我发现,即使将executeAsync超时设置为非常大的数字,如果不调用
this.async(timeout)
指定正确的超时(编写时默认为30秒),也会忽略此超时。因此,问题在于测试耗时超过30秒,传递给done的值没有返回到executeAsync的回调。

根据此处的Leadfoot文档

返回

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


从已执行的函数返回什么?

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);
                }); 
        }
    });
});

我正在返回一个数组,所以这应该不是问题。@cosmos1990你确定吗?看起来您可能实际上返回了一个承诺,最终解析为一个数组。绝对不是同一件事。那可能是。在任何情况下,我现在传入一个回调函数并调用它,而不是像@jason0x43所建议的那样返回一个值。我实现了它,因为这里有代码,但是
value
对于我来说仍然是
未定义的
。什么是
done
?正如我所说,
executeAsync
函数会自动传递一个回调作为它的最后一个参数。这个回调函数,您可以调用任何您想要的(我通常称之为“done”),当它完成任何操作时,应该由
executeAsync
函数调用。您不能从
executeAsync
函数返回承诺(当然可以,但Intern不会注意)。
executeAsync
函数将在浏览器中运行,承诺可能不可用,因此Intern使用回调而不是承诺来获取其返回值。因此,在将名为
done
的函数传递给传递给
executeAsync
脚本
参数后,然后调用
executeAsync
作为
executeAsync
中的最后一步,以表示它已完成执行,我希望传递给
executeAsync
回调的值(在本例中为
value
)是传递给
done
的值。但是
仍然是
未定义的
。我更新了我的示例以使其更加完整。此代码按原样工作(假设您的
index.html
页面与测试文件位于同一位置)。在这种情况下,使用setTimeout与使用Promission之间没有明显区别。如果使用值调用
done
方法,
executeAsync
将使用该值进行解析(只要它是可以序列化的内容)。我已经更新了我的示例,使用承诺,而不仅仅是一个简单的setTimeout,这很好。测试超时的事实表明,承诺链中的
then
方法没有被调用,这表明链上的某个地方抛出了错误。还可以尝试在
catch
块中调用done,以进行检查。