从 evalScript 函数返回变量的问题

Issue on returning a variable from an evalScript function

我正在为 Photoshop 制作 CEP HTML 面板,我想检查 首先,如果有任何 打开的文档 在我的面板执行它必须执行的操作之前。所以我在我的 index.js 中做了这样的事情来测试我是否得到了正确的结果。但是 psDocumentsLength 变量 returns 为未定义。知道我做错了什么吗?

(function()
{
   'use strict';

    var csInterface = new CSInterface();
    var psDocumentsLength; //1//

    function init()
    {
        themeManager.init();
        $(document).ready(function()
        {
            check_PSDocumentsLength();
            alert(psDocumentsLength); //4//
        });
    };

    init();

    function check_PSDocumentsLength() //2//
    {
        var chosenFunction = 'checkDocumentsLength()';
        csInterface.evalScript(chosenFunction, function(result)
        {
            psDocumentsLength = result; //3//
        });
    };

}());

考虑 Sergey Kritskiy saidgraphicdesign 关于 stackexchange 的问题在 code async 中,我尝试在 check_PSDocumentsLength() 函数调用之后添加一个 setTimeout,它成功了!!!所以我的代码现在看起来像这样...

    (function()
    {
       'use strict';

        var csInterface = new CSInterface();
        var psDocumentsLength; //1//

        function init()
        {
            themeManager.init();
            $(document).ready(function()
            {
                check_PSDocumentsLength();
                setTimeout(function()
                {
                    alert(psDocumentsLength);
                }, 1000); //4//
            });
        };

        init();

        function check_PSDocumentsLength() //2//
        {
            var chosenFunction = 'checkDocumentsLength()';
            csInterface.evalScript(chosenFunction, function(result)
            {
                psDocumentsLength = result; //3//
            });
        };

    }());