[英]Javascript Promise Chaining - Is it accepted?
我很擔心我的代碼,盡管它可以正常工作。
如標題所述,它被接受了嗎? 因為在我的數據庫中,我需要先完成Promise 1,然后才能繼續進行Promise 2,因為我需要訪問Promise 1的變量和結果。
簡而言之,我的數據庫中發生的事情是這樣的:
請注意,在login_tbl中,有一列是user_tbl的外鍵。 所以我必須先完成在user_tbl中的插入,否則會出現錯誤。
順便說一句,我正在使用postgresql,knex.js和bcrypt,這是我的代碼:
//This is the function that handles the signup
const handleSignup = (req, res, db, bcrypt) => {
const { employeeId, username, password, firstName, lastName, positionSelect } = req.body;
const hash = bcrypt.hashSync(password);
if (!employeeId || !username || !password || !firstName || !lastName || !positionSelect) {
res.json({
haveEmpty: true
})
}
else{
db.transaction((trx) => {
db.select('*').from('user').where('employee_id', '=', employeeId)
.then(data => {
if(!data[0]){
db('user')
.returning('*')
.insert({
employee_id: employeeId,
username: username,
first_name: firstName,
last_name: lastName,
status: "Active",
position_id: positionSelect
})
.then(user =>{
db('login')
.returning('*')
.insert({
employee_id: employeeId,
username: username,
hash: hash
})
.then(login => {
if(login[0]){
res.json({
isSuccess: true
})
}else{
res.json({
isSuccess: false
})
}
})
.then(trx.commit)
.catch(trx.rollback);
})
.then(trx.commit)
.catch(trx.rollback);
}
else {
res.json('User already Exist!')
}
})
.then(trx.commit)
.catch(trx.rollback);
})
.catch(err => console.error(err));
}
}
問題可能出在.then(data => {
部分。您正在那里創建一個新的Promise,但您沒有將它返回到另一個鏈接。我可能會發生,這個承諾將無法解決,因為包裝承諾不會進行任何嘗試,因為它不會返回。
您可以按以下方式更改代碼:
.then(data => {
if(!data[0]){
return db('user')
和
.then(user =>{
return db('login')
如果有一個承諾創建並沒有回來,第二天then
得到什么:
Promise.resolve('abc')
.then(res => { Promise.resolve(res.toUpperCase()); })
.then(res => console.log(res) /*prints undefined*/);
塊{ Promise.resolve(res.toUpperCase()); }
{ Promise.resolve(res.toUpperCase()); }
創建了一個Promise,但沒有返回任何內容,這意味着Promise不會進一步連鎖,也無法解決。
一切都很好,當諾言返回時,諾言進入了鏈條:
Promise.resolve('abc')
.then(res => { return Promise.resolve(res.toUpperCase()); })
.then(res => console.log(res) /*prints ABC*/);
在這種情況下.then(res => { return Promise.resolve(res.toUpperCase()); })
可以.then(res => Promise.resolve(res.toUpperCase()))
。
編輯:更多的諾言鏈解釋。
為了使其成為一個鏈,您的代碼應看起來像這樣:
db.transaction((trx) => {
return db.select('*').from('user').where('employee_id', '=', employeeId).then(data =>{
if(data[0]) {
res.json('User already Exist!')
//if it's possible use `return` instead `else`
return null;
}
return db('user')
.returning('*')
.insert({
employee_id: employeeId,
username: username,
first_name: firstName,
last_name: lastName,
status: "Active",
position_id: positionSelect
}).then(user=>{
return db('login')
.returning('*')
.insert({
employee_id: employeeId,
username: username,
hash: hash
})
}).then(login =>{
if(login[0]){
res.json({
isSuccess: true
})
}else{
res.json({
isSuccess: false
})
}
return true
}).then(trx.commit)
.catch(trx.rollback)
})
}).catch(err=> {
console.error(err)
res.status(500).json({error: true})
})
我建議閱讀有關Promises鏈的信息,例如:this: https ://javascript.info/promise-chaining
如果諾言不相互依賴? 這意味着您不需要第一筆諾言中的信息就可以獲取第二筆諾言,然后可以將它們鏈接在一起。
const Promise1 = fetch('https://API_URL_1');
const Promise2 = fetch('https:/API_URL_2');
Promise
.all([Promise1,Promise2])
.then(responses => {
return Promise.all(responses.map(res => res.json()))
})
.then(responses => {
console.log(responses)
})
.catch(err => {
console.error(error)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.