gpt4 book ai didi

javascript - 在 promise 中链接三个查询

转载 作者:行者123 更新时间:2023-11-29 22:56:18 25 4
gpt4 key购买 nike

我如何在一个 promise 中将这 3 个查询链接在一起?它们会一个接一个地运行还是并行运行?

SELECT * FROM books where book_id = $1

SELECT * FROM username where username = $2

SELECT * FROM saved where saved_id = $3

编辑:

我不知道我应该为函数 getBookIdQuery 返回什么。它给出了错误:

getBookIdQuery is not a function

function getBookIdQuery(){
return client.query(getBookId, [bookId]);
}

那么为了让我的链接工作,我应该为该函数返回什么?

return new Promise((resolve, reject) => {



getBookIdQuery
.then(data => {


})
.catch(err => {
reject(err);
});

});

最佳答案

不确定您使用的是哪个数据库。但总的来说,您可以按照您想要的方式串行或并行执行查询。

Serial:

async serial(){
queryResult1 = await client.query('SELECT * FROM books where book_id = $1', ['bookid']);
queryResult2 = await client.query('SELECT * FROM username where username = $2',['username']);
queryResult3 = await client.query('SELECT * FROM saved where saved_id = $3',['saved_id']);
console.log(queryResult1);
console.log(queryResult2);
console.log(queryResult3);
}


Parallerl:

async parallel(){
Promise.all([client.query('SELECT * FROM books where book_id = $1', ['bookid']), client.query('SELECT * FROM username where username = $2',['username']), client.query('SELECT * FROM saved where saved_id = $3',['saved_id']))
.then(queryResults => {
console.log(queryResults[0]);
console.log(queryResults[1]);
console.log(queryResults[2]);
})
}

关于javascript - 在 promise 中链接三个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56538192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com