此承诺处理模式是否有效?

时间:2017-07-14 07:56:20

标签: javascript design-patterns promise bluebird

公平警告,这是我第一次来到StackExchange。要温柔。

我有一个小的(我认为?)javascript项目(我用它来学习),它被分解为模块。我使用Bluebird作出更强有力的承诺。

每个模块都有自己独立的职责,并根据需要利用其他模块的功能。例如,我有一个控制器模块,用于管理用户注册表单和验证。该控制器与服务模块一起工作,该服务模块与紧邻的模块进行无人通信。然后该服务模块使用远程模块,该模块通过第三方库处理与整个应用程序的服务器的所有远程通信。该库产生了promises ...所以我的每个模块都处理它们从服务器查询返回的部分结果,因为它返回(在这种情况下)表单控制器。

问题是,我的代码的一部分是以某种方式设法不处理被拒绝的承诺。蓝鸟正在抛出一个" PossiblyUnhandledRejection"事件。在仔细检查了通常的嫌疑人(错别字,错过的分号,缺少括号,==而不是===,忘记返回/抛出/捕获等)并试图找到我是否正在做的明确确认有些不对劲(或者即使只有一个更标准化的"被接受的模式用于此)我还有bupkis。

看起来有点像这样:

/*** form controller ***/

/**
 * setup and control form display
**/

function isUnameAvailable(formData) {
    /**
     * do stuff with data to collected from the form (or not, depending on 
     * form data)
     *
     * formData -> userDetails
    **/

    return formService.getUser(userDetails)
        .then((data) => {
            /**
             * do stuff with data returned by the server
            **/

            // create new promise with results
            return data; 

        }).catch((err) => {
            /**
             * attempt to recover from the error.
            **/

            // handle promise as an error in all remaining cases, by logging 
            // it and displaying an error dialog
            eHandle.error(err);             

            // make it clear to bluebird that promises have been handled as 
            // intended (one of many "solutions" that have solved nothing)
            return; 

        });
}

/*** form service module ***/

/**
 * setup data to be held and provide methods for working with it
**/

function getUser(userDetails) {
    /**
     * do stuff with data to be sent to server (and more/or not, depending
     * on various considerations)
    **/

    return remote.find('user', userDetails)
        .then((data) => {
            /**
             * do stuff with data returned by server
            **/

            // if the results are properly formed and as expected create new 
            // promise containing data to be used by the controller
            return data;

        }).catch((err) => {

            // create new rejected promise to be handled further up the
            // chain. In this case, we can't do much with it here.
            throw err;

        }); 
}

/*** remote service module ***/

/**
 * setup 3rd party module to communicate as required with remote server
**/

function find(remoteModel, searchCriteria) {
    /**
     * Collect data about the remote request to held handle rejections
    **/

    return thirdPartyLib[ remoteModel ].find(searchCriteria)
        .then((data) => {
            /**
             * check and clean data returned by server
            **/

            // if the results are properly formed and as expected create
            // new promise containing data to be used by the form service
            return data;

        }).catch((err) => {
            /**
             * check remote error and try to recover if possible (e.g. retry 
             * the server if it makes sense to). if not possible, format
             * returned error data in a consistent form for use elsewhere in
             * the application
            **/

            // create new rejected promise containing our custom error
            // (which is, indeed, an instance of Error)
            throw new CustomError(modifiedError);

        });
}

Bluebird文档在这个问题上有点神秘,saying" ...因为在不确定的未来任何时候都可以处理被拒绝的承诺,一些编程模式会导致误报。因为这些编程模式不是必需的,并且总是可以重构以永远不会导致误报,所以我们建议这样做以尽可能简化调试。"

嗯,gee willikers先生......这当然是非常重要的,但它也完全没有用。这是其中一种模式吗?我在哪里可以找到答案?我应该使用应该的模式?几天谷歌搜索和浏览StackExchange后来我有一些消息来源似乎意味着我在做什么应该工作,但没有确切的答案我的具体问题。更糟糕的是,我上面描述的模式实际上在应用程序的其他部分中工作得很好,只是为了在这里神秘地发现错误。它实际上仍然有效...它只是在控制台上填充了不应该出现的错误。

Bluebird文档继续举例说明如何实施更复杂的"错误管理,尽管有这些含糊不清但又有些有害的模式,但它说的是......我得到的印象是,给出的建议更像是讽刺而不是实际的建议。

所以现在,我只是想知道我是否应该期待上面的模式工作......或者我是否需要重构应用程序(以及以何种方式)?如果我至少可以找出通往阿尔伯克基的方式,也许我可以从那里自己到达拉斯维加斯(或者我会回过头来弄清楚我做了什么其他错误的转变)。

0 个答案:

没有答案
相关问题