我似乎无法将查询与承诺链接在一起。最让我困惑的是.then(function(doSomething(部分。
我应该在函数中放入什么(做某事(?它有什么作用?
有人可以在不使用 Promise.all 而是使用 .then(( 的情况下为我链接这些查询吗?所以我可以从中学习
SELECT * FROM books where book_id = $1
SELECT * FROM username where username = $2
SELECT * FROM saved where saved_id = $3
function(doSomething)
在上一个承诺成功完成时运行,响应doSomething
。您可以使用如下then
链接承诺:
query("SELECT * FROM books where book_id = $1")
.then(() => query("SELECT * FROM username where username = $2"))
.then(() => query("SELECT * FROM saved where saved_id = $3"));
这将按顺序执行三个 SQL 查询。
但是,由于您很可能希望保存响应,因此为简单起见,您可以使用async/await
:
async function threeQueries() {
//Fetch all three queries in sequence
let queryOne = await query("SELECT * FROM books where book_id = $1");
let queryTwo = await query("SELECT * FROM username where username = $2");
let queryThree = await query("SELECT * FROM saved where saved_id = $3");
//Extract the response text from our queries
let resultOne = await queryOne.text();
let resultTwo = await queryTwo.text();
let resultThree = await queryThree.text();
//Return the responses from the function
return [resultOne, resultTwo, resultThree];
}
您也可以像这样使用Promise.all
:
Promise.all([
query("SELECT * FROM books where book_id = $1"),
query("SELECT * FROM username where username = $2"),
query("SELECT * FROM saved where saved_id = $3")
]);
Promise 的目的是实现对异步操作的更好流量控制。在以下情况下,必须按任意顺序完成多个任务,然后代码流才能继续。当您有多个异步任务时,请使用 Promise.then,其中每个步骤可能部分取决于前一个的结果(例如,在查询您的 books 表后,您使用 books.savedByUserId 查询用户名表以获取相应的用户记录(。
引用以下一些示例: https://codeburst.io/node-js-mysql-and-promises-4c3be599909b 作者提供了一个简单的mySql包装器,它返回Promise(database.query返回新的Promise(。
//In this example Promise.all executes the queries independently, but provides an
//effective tool to resume your work after all are completed. The order in which
//they complete may be random/indeterminate.
var bookQuery = database.query( 'SELECT * FROM books where book_id = $1' );
var userQuery = database.query( 'SELECT * FROM username where username = $2' );
var saveQuery = database.query( 'SELECT * FROM saved where saved_id = $3' );
Promise.all([bookQuery,userQuery,saveQuery]).then(function(results){
//resume whatever processing that should happen afterwards
//For instance, perhaps form fields in your UI require these datasets to be loaded
//before displaying the UI.
myDialog.open()
});
// In this example, things are done sequentially, this makes the most sense
// when the result of each operation feeds into the next. Since your queries don't
// rely on each other, this is not ideally depicted.
let bookRows, userRows, savedRows ;
database.query( 'SELECT * FROM books where book_id = $1' )
.then( rows => {
bookRows = rows;
return database.query( 'SELECT * FROM username where username = $2' );
})
.then( rows => {
userRows = rows;
return database.query( 'SELECT * FROM saved where saved_id = $3' );
})
.then( rows => {
savedRows = rows;
return database.close();
})
.then( () => {
// do something with bookRows, userRows, savedRows
}
.catch( err => {
// handle the error
})
附言不是为了搅浑水,但在这种情况下,三个顺序的 sql 查询可能会被一个带有连接的查询所取代,但我想这不是问题的重点。让我们假设它是三个查询来分隔商店,这是有道理的。
我们使用 promise 这些对象,可能会在未来某个时候产生单个值。
当你运行query("query string")
时,它将异步返回一个 Promise 对象。这意味着,你的应用不会等待查询完成。它将启动查询过程并转到下一行代码。
那么,当查询完成后,我们如何处理查询呢?
我们使用then
来处理查询返回的信息。 当查询成功完成其进程时,将触发then
。