gpt4 book ai didi

javascript - Promise 内部的 Promise 在 bluebird Nodejs 中创建难以管理的结构

转载 作者:行者123 更新时间:2023-12-03 03:53:19 25 4
gpt4 key购买 nike

我的查询如下所示:

checkUserId(user_email).then((info) => {
changeBookStatus(bookInfo._id, borrow).then(() => {
issueReturnBook(info._id, bookInfo._id,
due_date + "/" + due_month, borrow).then((savedInfo) => {
console.log("saved info " + savedInfo._id);
LibraryTransaction.findById(savedInfo._id).
populate('UserDetails').populate('BookDetails').exec((err, libInfo) => {
res.json({
status: '200',
message: 'transaction completed',
data: libInfo
});
});

}).catch((err) => {
res.json({
status: '500',
message: err
});
});
}).catch((err) => {
res.json({
status: '500',
message: err
});
});
})


const issueReturnBook = (user_id, book_id, due_date, borrow) => {
let borrowReturn;
if (borrow == '0') {
borrowReturn = false;
} else if (borrow == '1') {
borrowReturn = true;
}
return new Promise((resolve, reject) => {
const libraryTransaction = new LibraryTransaction();
libraryTransaction.UserDetails = user_id;
libraryTransaction.BookDetails = book_id;
libraryTransaction.DueDate = due_date;
libraryTransaction.BorrowReturn = borrowReturn;
libraryTransaction.save().then((info) => {
if (!info) {
reject(false);
}
resolve(info);
}).catch((err) => {
reject(err);
});
});
};


const changeBookStatus = (book_id, borrow) => {
let borrowStat;
if (borrow == '0') {
borrowStat = false;
} else if (borrow == '1') {
borrowStat = true;
}
return new Promise((resolve, reject) => {
Books.findOneAndUpdate({ '_id': book_id }, { $set: { CurrentlyAvailableStatus: borrow } }).then((info) => {
if (!info) {
reject('there is no such book');
}
resolve(info);
}).catch((err) => {
reject(err);
});
});
};

const checkUserId = (user_email) => {
return new Promise((resolve, reject) => {
User.findOne({ 'Email': user_email.trim() }).then((info) => {
if (!info) {
reject('the is no such user');
}
resolve(info);
}).catch((err) => {
reject(err);
});
});
};

现在 then 里面的 then 太多了,有没有更好的方法来高效地做到这一点?

最佳答案

辨别哪些变量在作用域内以及哪些 LibraryTransaction 函数返回 Promise 有点困难。但我认为你想要这样的东西:

checkUserId(user_email)
.then(info => {
return changeBookStatus(bookInfo._id, borrow)
// This is here so we can return "info" to the next function. You might want to
// wrap this "changeBookStatus(...).then(...)" part in its own function.
.then(() => {
return info;
});
})
.then(info => {
return issueReturnBook(info._id, bookInfo._id, due_date + "/" + due_month, borrow);
})
.then(savedInfo => {
console.log("saved info " + savedInfo._id);
LibraryTransaction
.findById(savedInfo._id)
.populate('UserDetails')
.populate('BookDetails').exec((err, libInfo) => {
res.json({
status: '200',
message: 'transaction completed',
data: libInfo
});
});

})
.catch(err => {
res.json({
status: '500',
message: err
});
});

请注意,我已经添加了 return 语句,以明确每个 Promise 都需要返回才能正确链接。但您可以删除它们来清理内容:

checkUserId(user_email)
// The extra ".then" is still here for the reasons mentioned above
.then(info => changeBookStatus(bookInfo._id, borrow).then(() => info))
.then(info => issueReturnBook(info._id, bookInfo._id, due_date + "/" + due_month, borrow))
.then(savedInfo => {
console.log("saved info " + savedInfo._id);
LibraryTransaction
.findById(savedInfo._id).
populate('UserDetails')
.populate('BookDetails').exec((err, libInfo) => {
res.json({
status: '200',
message: 'transaction completed',
data: libInfo
});
});

})
.catch(err => {
res.json({
status: '500',
message: err
});
});

关于javascript - Promise 内部的 Promise 在 bluebird Nodejs 中创建难以管理的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45077921/

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