承诺内部承诺在蓝鸟nodejs中创建难以管理的结构



我的查询如下所示:

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);
});
});
};

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

要判断哪些变量在范围内,以及哪些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语句,以明确每个承诺都需要返回才能正确链接。但是您可以删除它们来清理内容:

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
});
});

相关内容

  • 没有找到相关文章

最新更新