开卷承诺和他们的处理程序 [英] Unwinding promises and their handlers

查看:109
本文介绍了开卷承诺和他们的处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要启动的任务的顺序,我需要他们的顺序执行。当一个按钮(#submitButton)被点击的整个过程展开(startAll功能),所以我实现这样的架构:

I want to launch a sequence of tasks and I need they execute in order. When a button (#submitButton) is clicked on the entire process is launched (startAll function), so I implement an architecture like this:

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

高/ lowPriorityTasks方法有这样的结构:

high/lowPriorityTasks methods have a structure like this:

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. 函数初始化,启动,完成和终止,他们最后的命令返回;
    但我不知道这是否是正确的。

  2. 我使用jQuery 1.10.2和asp.net MVC 4

发现的问题


  1. 在执行它,将引发一个运行时错误说,对象
    在行不接受属性或方法',那么'粗体:

  1. When executing it, a runtime error is raised saying that the object does not accept the property or method 'then' at line bolded:

highPriorityTasksDeferred = startDeferred.then(初始化).done(启动);

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

第一次尝试

由于肯尼斯说,$ .Deferred后的括号中错过了加入他们$ .Deferred()被执行的过程,但出现了一些问题:它看起来像,高/ lowPriorityTasks功能不作为控制台执行显示:

As kenneth has said, brackets after $.Deferred were missed so by adding them $.Deferred() the process is executed but some issues appears: It seems like, the high/lowPriorityTasks functions are not executed as in the console is displayed:

Initiating process...
Process started at XXX


Finishing process...

Process terminated at XXX

任何想法?此外,我想知道如果返回;在功能初始化,初始化,完成并终止是做到这一点的最好办法还是有另一种最佳的选择。

Any ideas? Also i would like to know if the return; in the functions initialize, initiate, finalize and terminate is the best way to do it or there is another best option.

第二次尝试

有些托槽错过highPriorityTasks和lowPriorityTasks调用,所以现在他们正在正确执行的时候,但问题是,他们不是为了做....

Some brackets were missed for highPriorityTasks and lowPriorityTasks when called so now they are executed correctly but the problem is that they are not done in order....

推荐答案

您错过括号后 $。递延。目前,它不返回延期功能的延迟对象。

You're missing brackets after the $.Deferred. At the moment it's returning the deferred-function not a Deferred-object.

将其更改为下面的,它应该工作:

Change it to the following and it should work:

startDeferred = $.Deferred();

这篇关于开卷承诺和他们的处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆