【问题标题】:Different syntax of defining promises定义承诺的不同语法
【发布时间】:2016-05-11 13:11:15
【问题描述】:

我正在学习 JavaScript 的承诺。在这个code 中有一件事让我很困惑:

从第 42 行开始,return ctx.sync(); 之后是 }),然后是 .then(function() {

    Excel.run(function (ctx) {
        // Create a proxy object for the worksheets collection
        var worksheets = ctx.workbook.worksheets;

        // Add 14 sheets to the workbook
        for (var i = 2; i <= 15; i++) {
            // Queue commands to add new sheets to the workbook
            worksheets.add("Sheet" + i);
        }

        //Disable the button
        $('#add-sheets').prop('disabled', true);


        //Run the queued-up commands, and return a promise to indicate task completion
        return ctx.sync();

    })
    .then(function () {
        // Now that we have sheets, create buttons for each sheet
        // in the taskpane to enable switching
        createSheetButtons();
    })
    .catch(function (error) {
        // Always be sure to catch any accumulated errors that bubble up from the Excel.run execution
        app.showNotification("Error: " + error);
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });

但是,从第 75 行开始,return ctx.sync() 后面紧跟着 .then(function() {

    Excel.run(function (ctx) {
        // Create a proxy object for the worksheets collection 
        var worksheets = ctx.workbook.worksheets;

        // Queue a command to load the name property of each worksheet
        worksheets.load("name");

        //Run the queued-up commands, and return a promise to indicate task completion
        return ctx.sync()
            .then(function () {
                //create a button for each sheet in the task pane
                for (var i = 0; i < worksheets.items.length; i++) {
                    var buttonName = worksheets.items[i].name;
                    var $input = $('<input type="button" class="ms-Button" value=' + buttonName + '>');
                    $input.appendTo($("#buttons-div"));
                    // Add a click event handler for the button
                    (function (buttonName) {
                        $input.click(function (e) {
                            makeActiveSheet(buttonName);
                        });
                    })(buttonName);
                }
            });
    })
    .catch(function (error) {
        // Always be sure to catch any accumulated errors that bubble up from the Excel.run execution
        app.showNotification("Error: " + error);
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });

谁能告诉我这种微妙的语法是否会对这两个 Promise 的定义产生影响?

【问题讨论】:

  • 我的问题有什么问题?是不是太容易了?
  • 看来您已经外包了对理解您的问题至关重要的数据。请记住在问题本身中提供所有必要的数据(代码、配置数据、异常名称...)。如果链接失效或更改,您的问题将失去大部分(如果不是全部)意义!您可能有兴趣阅读help center,尤其是How to Ask 以及什么是minimal reproducible example
  • SoftTimur:仔细看看你链接的代码的缩进。
  • 因为一个是返回一个promise,一个是使用返回的promise。
  • @SoftTimur:一行代码自己返回一个承诺,另一行返回一个添加了对.then() 的调用的承诺。 .then() 充当异步操作完成后要调用的“回调”。所以第一行基本上是说“这是一个异步操作”,第二行是说“这是一个异步操作,还有一个后续操作”。

标签: javascript promise


【解决方案1】:

两个函数(.run().sync())表面上都返回 Promise,每个函数的完成只是在不同的地方捕获,语法相同。

Excel.run(function (ctx) {
            // Create a proxy object for the worksheets collection
            var worksheets = ctx.workbook.worksheets;

            // Add 14 sheets to the workbook
            for (var i = 2; i <= 15; i++) {
                // Queue commands to add new sheets to the workbook
                worksheets.add("Sheet" + i);
            }

            //Disable the button
            $('#add-sheets').prop('disabled', true);


            //Run the queued-up commands, and return a promise to indicate task completion
            return ctx.sync();

        })
        .then(function () {
            // Now that we have sheets, create buttons for each sheet
            // in the taskpane to enable switching
            createSheetButtons();
        })

这将在 .run() 返回的承诺上运行 .then() 而在

return ctx.sync()
                .then(function () {
                    //create a button for each sheet in the task pane
                    for (var i = 0; i < worksheets.items.length; i++) {
                        var buttonName = worksheets.items[i].name;
                        var $input = $('<input type="button" class="ms-Button" value=' + buttonName + '>');
                        $input.appendTo($("#buttons-div"));
                        // Add a click event handler for the button
                        (function (buttonName) {
                            $input.click(function (e) {
                                makeActiveSheet(buttonName);
                            });
                        })(buttonName);
                    }
                });

.then() 正在根据从 .sync() 返回的承诺运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多