Javascript pgpromise:链接条件查询
我试图找到链接条件查询的正确方法 下面是一些伪代码来说明我的情况:Javascript pgpromise:链接条件查询,javascript,express,bluebird,pg-promise,Javascript,Express,Bluebird,Pg Promise,我试图找到链接条件查询的正确方法 下面是一些伪代码来说明我的情况: check whether the an item exists; if no: reply with status 404; if yes: check whether the user owns the item; if no: redirect to another page; if yes: retrieve the information
check whether the an item exists;
if no:
reply with status 404;
if yes:
check whether the user owns the item;
if no:
redirect to another page;
if yes:
retrieve the information about the item and render the page;
我的第一个直觉是为了重用相同的连接,但由于可能的结果不同,我很难想出如何正确处理承诺:
db.task(t => {
return t.items.exists(itemId)
.then(exists => {
if (!exists) { // item does not exist
// 404
}
return t.items.isOwner(itemId, userId)
.then(isOwner => {
if (!isOwner) {
// redirect to some other page
}
return t.items.getById(itemId);
})
})
})
.then(data => {
// OK, process data and render
})
.catch(console.error); // unexpected errors
例如,如果我尝试重定向到404页面,承诺仍然会在以后得到解决。
另一种方法是采用以下方法:
if (!exists) { // item does not exist
return Promise.reject('404');
}
...
.then(data => {
// OK, process data and render
}, reason => {
// KO, conditions were not met to resolve
})
“有效”,但同时捕获错误和未满足的条件。我希望有一个专门的“未满足条件”处理程序
我想到的另一个方法是:
var p = db.task(t => {
return t.items.exists(itemId)
.then(exists => {
if (!exists) { // item does not exist
// resolve p (and break promise chain) with something like
// p.resolve(() => {
// return res.redirect...
// });
}
// else we can go on with the queries
return t.items.isOwner(itemId, userId);
}
.then(isOwner => {
if (!isOwner) {
// resolve p and redirect to some other page
}
return t.items.getById(itemId);
})
.then(item => {
// everything OK, resolve with a handler to render the page
});
})
.then(action => {
action();
})
.catch(console.error); // unexpected errors
但是我看不出有什么办法可以解决p
。在嵌套承诺中调用Promise.resolve(…)
会在通过p
的之后解决下一个承诺本身
在关注性能的同时,在pg promise
中链接条件查询和处理不同结果的推荐方法是什么?看看这是否适合您
这仍然需要该项完成每个承诺,直到它到达最后一个然后
块或捕获
块
// First Condition
const input = 'test input'
Promise.resolve({ item: input })
// Repeat this for different condition on `item.item` and change `X`
/* REPEAT START */
.then(data => {
if (data.hitCondition != null && data.hitCondition !== '') {
return data;
}
if (conditionX(data)) {
return Object.assign({}, data, {
hitCondition: 'conditionX'
});
}
return data;
})
/* REPEAT END */
...
.then(result => {
const { item, hitCondition } = result
if (hitCondition != null && hitCondition !== '') {
// at least one condition is met
// check hitCondition to see which condition is hit
} else {
// none of the conditions are met
}
})
.catch(error => {
// Some error happened somewhere
console.error(error);
});
作者的问题主要是使用承诺,而不是太多
在我的场景中效果很好,谢谢。我想知道,随着承诺的增加和负载的增加,失败是否会对性能产生影响。我还是喜欢一种不太老套的方式,但同时我很乐意接受你的方式;)
db.task('easy-peasy', async t => {
if (await t.items.exists(itemId)) {
// reply with status 404;
} else {
if (await t.items.isOwner(itemId, userId)) {
// redirect to some other page
} else {
return t.items.getById(itemId); // return the data
}
}
})
.then(data => {
if (data) {
// Task returned data;
// Render the data;
}
})
.catch(console.error); // unexpected errors