在 knex 中,如何将 async/await 函数更改为使用 .then 方法的函数?
In knex, how do I change an async/await function to one that uses a .then method?
我正在 knex.js 中编写一个简单的 post 路由,用于将消息添加到数据库。目前它是一个 async/await 函数,但我如何使用 .then 块重构它?代码正在运行,我只是好奇。代码如下:
const todo = req.body.message;
try {
await db('todos').insert({message: todo});
res.json({message: 'todo successfully stored'});
} catch (err) {
console.log(err);
}
谢谢!
我有理由相信 async
只是 return 一个承诺,所以你可以简单地删除 await
const todo = req.body.message;
try {
db('todos')
.insert({message: todo})
.then((result) => {
res.json({message: 'todo successfully stored'});
});
} catch (err) {
console.log(err);
}
我正在 knex.js 中编写一个简单的 post 路由,用于将消息添加到数据库。目前它是一个 async/await 函数,但我如何使用 .then 块重构它?代码正在运行,我只是好奇。代码如下:
const todo = req.body.message;
try {
await db('todos').insert({message: todo});
res.json({message: 'todo successfully stored'});
} catch (err) {
console.log(err);
}
谢谢!
我有理由相信 async
只是 return 一个承诺,所以你可以简单地删除 await
const todo = req.body.message;
try {
db('todos')
.insert({message: todo})
.then((result) => {
res.json({message: 'todo successfully stored'});
});
} catch (err) {
console.log(err);
}