Javascript 在承诺链中抛出不同的错误?
我在WebApp服务器中有一个使用节点的方法,该节点将执行多个异步操作,例如:Javascript 在承诺链中抛出不同的错误?,javascript,asynchronous,promise,Javascript,Asynchronous,Promise,我在WebApp服务器中有一个使用节点的方法,该节点将执行多个异步操作,例如: function saveTopValuePages( pageSize, clientId ) setExistingCredentials(clientId) .then(function() { return getViewIdByClientId(clientId); }, function(error) { t
function saveTopValuePages( pageSize, clientId )
setExistingCredentials(clientId)
.then(function() {
return getViewIdByClientId(clientId);
}, function(error) {
throw new customErrors.SetExistingCredentials('test123')
})
.then(function(viewId) {
return fetch(viewId, options)
}, function(error) {
throw new customErrors.GetViewIdByClientId('abcasdasd')
})
.then(function(response) {
return response;
})
之后,我将从我的路线调用此功能:
analytics.saveTopValuePages(pageSize, clientId)
.then(function(data) {
res.status(200)send({ message: 'success'})
}
}).catch(customErrors.SetExistingCredentials, function(error) {
res.status(400).send({ error: error})
}).catch(customErrors.GetViewIdByClientId, function(error) {
res.status(401).send({error: error})
}).catch(function(e){
res.status(500).send({ error: "Unknown internal server error" });
});
正如您在我的示例中所看到的,我试图抛出一个错误,这取决于进程的哪个部分失败了。如果所有这些动作都是相互独立的,那么这将是可行的,但事实恰恰相反。因此,如果setExistingCredentials失败,以下所有函数都将失败
这导致如果setExistingCredentials失败,将抛出错误以及GetViewIdByClientId错误。最后,我将捕获的错误是本例中最后一个GetViewIdByClientId错误,而不是正确的错误
因此,我不确定这是否是一个正确的方法,也不确定是否有任何其他模式可以实现我所需要的结果
提前谢谢 不知道这有多洁净
function saveTopValuePages( pageSize, clientId ) {
setExistingCredentials(clientId)
.then(function() {
return getViewIdByClientId(clientId);
})
.catch(function(error) {
throw { custom: customErrors, type: 'SetExistingCredentials', arg: 'test123' }
})
.then(function(viewId) {
return fetch(viewId, options)
})
.catch(function(error) {
if (error.custom == customErrors) {
throw new customErrors[error.type](error.arg);
}
throw new customErrors.GetViewIdByClientId('abcasdasd')
});
// next 3 lines are actually redundant
//.then(function(response) {
// return response;
//})
}
如果这不起作用,那么你不会喜欢它-我不喜欢它-但有时它必须做-筑巢
function saveTopValuePages( pageSize, clientId ) {
setExistingCredentials(clientId)
.then(function() {
return getViewIdByClientId(clientId).then(function(viewId) {
return fetch(viewId, options)
}, function(error) {
throw new customErrors.GetViewIdByClientId('abcasdasd')
});
}, function(error) {
throw new customErrors.SetExistingCredentials('test123')
})
.then(function(response) {
return response;
})
}
当然,您可以通过创建一个函数来减轻这种不愉快
function getView(clientId) {
return getViewIdByClientId(clientId).then(function(viewId) {
return fetch(viewId, options)
}, function(error) {
throw new customErrors.GetViewIdByClientId('abcasdasd')
});
}
function saveTopValuePages( pageSize, clientId ) {
setExistingCredentials(clientId)
.then(function() {
return getView(clientId);
}, function(error) {
throw new customErrors.SetExistingCredentials('test123')
})
.then(function(response) {
return response;
})
}
为什么不使用本机错误?var error=新错误发生错误;投掷误差;我在前面尝试过这种方法,但它是相同的,最后一个错误是传播到catch的错误,因此它始终是getViewIdByClientId错误。如果依赖的操作抛出,后续操作将如何运行?它们不应该运行,例如,如果无法设置凭据,则其他功能将无法实现。一旦承诺链中的承诺被拒绝,该链中的其他功能将不会执行。因此,第一次拒绝会导致链停止。除非你明确承认一个错误并让这个链继续下去,否则永远不会有一个以上的错误。所以我不清楚您真正想要解决的问题是什么。是的,我一直在考虑将嵌套作为可能的解决方案,但我最近开始使用promises,我想知道是否有更优雅的解决方案/模式来处理此类情况。谢谢