2016-05-10 2 views
-2

私はJavaScriptを約束しています。約束を定義する異なる構文

ライン42、return ctx.sync();から、その後}).then(function() {が続いている:私のパズルこのcodeで一つのことがあります。

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() {によって直接続いています。構文のこの繊細は、これら2つの約束の定義との間の差異を行う場合

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

誰も教えてもらえますか?

+0

から返されたのに対し、.run()から返さ?あまりにも簡単ですか? – SoftTimur

+5

質問を理解するために重要なデータを委託しているようです。 **質問自体に必要なすべてのデータ(コード、構成データ、例外名...)を提供することを忘れないでください**。リンクが消えたり変更されたりすると、その意味のすべてではなくても、あなたの質問はほとんど失われます!あなたは[ヘルプ]、特に[尋ねる]と[mcve]とを読むことに興味があるかもしれません。 –

+0

SoftTimur:リンクしたコードのインデントをよく見てください。 – Cerbrus

答えて

0

どちらの関数も(.run().sync())表面上は約束を返し、それぞれの補完は別の場所で捕捉されているだけで、構文は同じです。約束の.then()を実行します

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

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

関連する問題