我有一个简单的“ then链”,该链运行一些功能性步骤。如果满足某些条件,我需要取消链并退出(主要是如果发生错误)。我在firebase云功能中使用了此功能,但我认为这是适用于任何节点/表达环境的基本概念。
这是我的代码:
let p1 = db.collection('products').doc(productId).get();
let p2 = db.collection('user_data').doc(userUid).get();
let promises= [p1,p2];
return Promise.all(promises)
.then(values =>
{
let proudctDoc = values[0];
let userDataDoc = values[1];
if(!proudctDoc.exists)
{
console.log("The product does not exist");
response.status(404).json({error: ErrorCodes.UNKNOWN_PRODUCT, msg: "The products does not exist"});
throw("CANCEL");
}
if(!userDataDoc.exists)
{
console.log("User data block not found!");
response.status(404).json({error: ErrorCodes.UNKNOWN_USER_DATA, msg: "User data block not found!"});
throw("CANCEL");
}
variantCountryRef = db.doc('products/'+productId+'/variants/'+variantCountry);
return variantCountryRef.get();
})
.then(variantCountryDoc =>
{
....
})
.catch(err =>
{
if(err !== "CANCEL")
{
//Deal with real error
}
}
如您所见,我只执行了2个诺言并等待它们完成。此后,我检查一些返回的值,并在发生错误时通知客户端。此时,我必须完成“然后链”。
这是常见的模式吗?我有什么可以改善的吗?
答案 0 :(得分:0)
您可以执行以下操作:
select project_creation_date, count(*) as day_count,
sum(count(*)) over (order by project_creation_date) as cumulative_count
from (select project_namespace, min(project_creation_date) as project_creation_date
from t
order by project_namespace, project_creation_date
) t
group by project_creation_date
order by project_creation_date;