定义承诺的不同语法

时间:2016-05-10 13:07:57

标签: javascript promise

我正在学习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));
        }
    });

有人能告诉我这种语法的微妙之处是否会对这两个承诺的定义产生任何影响?

1 个答案:

答案 0 :(得分:0)

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

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

.then()返回的承诺运行.run(),而

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()