简体   繁体   English

如何在节点js中打破promise-then链

[英]How to break out of promise-then chain in node js

I am working on SOA project where I am writing a node js utility to get my things done. 我正在研究SOA项目,我正在编写一个节点js实用程序来完成我的工作。 The utility starts with login to server using a SOA call then call a series of SOA call where each SOA call depends on the result of the previous call. 该实用程序首先使用SOA调用登录到服务器,然后调用一系列SOA调用,其中每个SOA调用都依赖于前一次调用的结果。

I have used promise-then framework to achieve this. 我使用了promise-then框架来实现这一目标。 Below is my code: 以下是我的代码:

const loginPromise = postRequest(req, headersObj, loginUrl, Constants.loginData);
const queryPromise = loginPromise.then(loginSuccess, loginFailure);
const searchPromise = queryPromise.then(handleSavedQuerySearchResponse, onFailure);
const createPromise = searchPromise.then(handleSearchSoaResponse, onFailure);
const updatePromise = createPromise.then(handleCreateObjectSoaResposne, onFailure);
const plantDataPromise = updatePromise.then(handleUpdateObjectSoaResponse, onFailure);

function postRequest(request: any, header: any, reqUrl: string, jsonData: any): Promise<any> {
    return new Promise((resolve) => {
        request.post({
            headers: header,
            url: reqUrl,
            body: JSON.stringify(jsonData),
        }, (err: any, resp: any, bodyParam: any) => {
            resolve({error: err, response: resp, body: bodyParam});
        });
    });
}

On successful execution of the one SOA the next SOA gets executed from the callback function. 成功执行一个SOA后,下一个SOA将从callback函数执行。 Everything works fine when there is no error scenario. 没有错误情况时,一切正常。 But if the server is down or in any other failure scenario I want to get out of this promise-then chain. 但是如果服务器停机或者在任何其他故障情况下我想摆脱这个promise-then链。

Currently on any error the flow gets to onFailure method but after its execution it gives the control to next promise in the chain with null as input for that method. 当前在任何错误上,流都会转到onFailure方法,但在执行之后,它会将控件赋予链中的下一个promise,并将null作为该方法的输入。

I want to get out of the promise-then chain whenever any error is encountered and rest of the promise should not get executed. 我想在遇到任何错误时退出promise-then链,并且不应该执行其余的promise。 I would appreciate if anyone could help me with this. 如果有人能帮助我,我将不胜感激。

This code has not been tested and should as such be though of more as pseudocode than an actual solution: 此代码尚未经过测试,因此应该更像伪代码而不是实际解决方案:

Try to either restructure your code more like this: 尝试重新构建您的代码更像这样:

const request = require('request-promise-native')

postRequest(req, headersObj, loginUrl, Constants.loginData)
    .then(loginSuccess)
    .then(handleSavedQuerySearchResponse)
    .then(handleSearchSoaResponse)
    .then(handleCreateObjectSoaResposne)
    .then(handleUpdateObjectSoaResponse)
    .catch(console.error)

function postRequest(request: any, header: any, reqUrl: string, jsonData: any): Promise<any> {
    return request.post({
        headers: header,
        url: reqUrl,
        body: JSON.stringify(jsonData),
    }, (err: any, resp: any, bodyParam: any) => {
            resolve({error: err, response: resp, body: bodyParam});
    });
}

Where the error handling is a part of the promise chain. 错误处理是promise链的一部分。

Or you can do pretty much the same with async/await. 或者你可以使用async / await做同样的事情。

You could add your onFailure function here instead of just console.error if you want to do some actual error handling 如果你想做一些实际的错误处理,你可以在这里添加你的onFailure函数,而不仅仅是console.error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM