解除承诺及其处理程序

时间:2013-09-24 12:56:08

标签: jquery asp.net-mvc-4 scripting

我想启动一系列任务,我需要按顺序执行。当单击一个按钮(#submitButton)时,启动整个进程(startAll函数),所以我实现了这样的架构:

function startAll()
{
   var startDeferred, highPriorityTasksDeferred, lowPriorityTasksDeferred, finalizeDeferred;

   startDeferred = $.Deferred;

   **highPriorityTasksDeferred= startDeferred.then(initialize).done(initiate); <-- ERROR HERE**

   lowPriorityTasksDeferred= highPriorityTasksDeferred.then(function () {
            console.log('Processing HIGH priority tasks...');
            return highPriorityTasks;
   })
   .done(function () {
            console.log('HIGH priority taks processed.');
   });

   finalizeDeferred= lowPriorityTasksDeferred.then(function () {
            console.log('Processing LOW priority tasks...');
            return lowPriorityTasks;
   })
   .done(function () {
            console.log('LOW priority taks processed.');
   });

   finalizeDeferred.then(finalize).done(terminate);

   // Starts all
   startDeferred.resolve();
}

function initialize() {
    // Things to be initialized just before starting process
    console.log('</br>Initiating process...');

    // Stuff

    return;
}

function initiate() {
    // Once initialized, do simple things
    setButtonDisabled(true);

    console.log('Process started at ' + $.now()+ '<br/>');

    return;
}

function finalize() {
    // Things to be done just before ending process
    // Free resources, etc.
    console.log('<br/>Finishing process...');

    // Stuff

    return;
}

function terminate() {
    // Things to be done once finished is done.
    // Simple things such as those related to UI.
    setButtonDisabled(false);

    console.log('<br/>Process terminated at ' + $.now());

    return;
}

function setButtonDisabled(status) {
    $('#submitButton').prop("disabled", status);
} 

high / lowPriorityTasks方法的结构如下:

function high/lowPriorityTasks() {

    getTasks().then(function (response) {

       // stuff

    }).then(function () {

       // stuff 

    }).fail(function (err) {

       // stuff
    });
}

控制台输出应为:

Initiating process...
Process started at XXX

Processing HIGH priority tasks...
HIGH priority taks processed.

Processing LOW priority tasks...
LOW priority taks processed.

Finishing process...
Process terminated at XXX

重要提示

  1. 函数初始化,启动,终止和终止,它们的最后一个命令是return; 但我不确定它是否正确。
  2. 我使用的是jQuery 1.10.2和asp.net MVC 4
  3. 观察到的问题

    1. 执行时,会引发运行时错误,说明该对象 不接受属性或方法'then'在粗线:

      highPriorityTasksDeferred = startDeferred.then(initialize).done(initiate);

    2. 首次尝试

      正如kenneth所说的那样,在$ .Deferred之后的括号被错过,所以通过添加$ .Deferred()过程被执行但是出现了一些问题:好像,高/低优先级的任务函数没有在控制台中执行显示:

      Initiating process...
      Process started at XXX
      
      
      Finishing process...
      
      Process terminated at XXX
      

      有什么想法吗?另外我想知道是否归来;在函数初始化,启动,终止和终止是最好的方法,或者有另一个最佳选择。

      第二次尝试

      调用highPriorityTasks和lowPriorityTasks时会遗漏一些括号,所以现在它们被正确执行但问题是它们没有按顺序完成....

1 个答案:

答案 0 :(得分:1)

$.Deferred之后你缺少括号。目前它正在返回延迟函数而不是Deferred-object。

将其更改为以下内容,它应该有效:

startDeferred = $.Deferred();