admin管理员组文章数量:1333621
I have a question about catching user error in async and wait.
Let say I have a route that only fetches for a single user.
routes.js
routes.get('/getuserbyid/:id', (req, res) => {
const id = req.params.id;
accountController.getById(id)
.then((result) => {
res.json({
confirmation: 'success',
result: result
});
})
.catch((error) => {
res.json({
confirmation: 'failure',
error: error
});
});
});
I have a controller that fetches the request. accountController.js
export const getById = async (id) => {
try {
const user = await users.findOne({ where: {
id: id
}});
if (user === null) {
return 'User does not exist';
}
return user;
} catch (error) {
return error;
}
}
So no matter what happens either I get a null or a single record. It is still a success. In promises, I can reject null, so it will show up in the catch block in the route. Now that with async and await. How can I achieve the same making the null in the error block?
export const getById = (id) => {
return new Promise((resolve, reject) => {
users.findOne({ where: {
id: id
}})
.then((result) => {
if (result === null) {
reject('User does not exit');
}
resolve(result);
})
.catch((error) => {
reject(error);
});
});
}
I have a question about catching user error in async and wait.
Let say I have a route that only fetches for a single user.
routes.js
routes.get('/getuserbyid/:id', (req, res) => {
const id = req.params.id;
accountController.getById(id)
.then((result) => {
res.json({
confirmation: 'success',
result: result
});
})
.catch((error) => {
res.json({
confirmation: 'failure',
error: error
});
});
});
I have a controller that fetches the request. accountController.js
export const getById = async (id) => {
try {
const user = await users.findOne({ where: {
id: id
}});
if (user === null) {
return 'User does not exist';
}
return user;
} catch (error) {
return error;
}
}
So no matter what happens either I get a null or a single record. It is still a success. In promises, I can reject null, so it will show up in the catch block in the route. Now that with async and await. How can I achieve the same making the null in the error block?
export const getById = (id) => {
return new Promise((resolve, reject) => {
users.findOne({ where: {
id: id
}})
.then((result) => {
if (result === null) {
reject('User does not exit');
}
resolve(result);
})
.catch((error) => {
reject(error);
});
});
}
Share
Improve this question
edited Feb 9, 2018 at 7:14
Felix Kling
817k181 gold badges1.1k silver badges1.2k bronze badges
asked Feb 8, 2018 at 20:28
Pak ChuPak Chu
2531 gold badge5 silver badges13 bronze badges
2
-
3
You would
throw null;
– Aluan Haddad Commented Feb 8, 2018 at 20:30 -
I can see a couple problems here. First, the strict test
(user === null)
is likely missing some error cases, for exampleundefined
. Secondly, the try/catch is unnecessary and can go away. Ifuser
is anything besides a valid user object, simply throw an error right then and there. It will be handled by your.catch()
callback above. – greim Commented Feb 8, 2018 at 20:41
3 Answers
Reset to default 7First, avoid the Promise antipattern. You have a function that returns a Promise, no need to wrap that in a new Promise()
.
Then your funciton could look like this:
export const getById = (id) => {
return users.findOne({ where: { id } })
.then((user) => {
if (user === null)
throw 'User does not exit';
return user;
});
}
and the async/await version of this is
export const getById = async (id) => {
const user = await users.findOne({ where: { id } });
if(user === null)
throw 'User does not exist';
return user;
}
An async
function always returns a Promise
.
Using the throw
construct within an async
function rejects the Promise
that is returned.
Consider
function getValue() {
return Promise.reject(null);
}
getValue().catch(e => {
console.log('An raised by `getValue` was caught by a using the `.catch` method');
console.log(e);
});
(async function main() {
try {
await getValue();
} catch (e) {
console.log('An raised by `getValue` was caught by a catch block');
console.log(e);
}
}());
async function getValue() {
throw null;
}
getValue().catch(e => {
console.log('An raised by `getValue` was caught by a using the `.catch` method');
console.log(e);
});
(async function main() {
try {
await getValue();
} catch (e) {
console.log('An raised by `getValue` was caught by a catch block');
console.log(e);
}
}());
A try
block within an async method handles both synchronous and asynchronous errors.
Consider
function throws() {
throw Error('synchronous failure');
}
async function rejects() {
throw Error('asynchronous failure');
}
(async function main() {
try {
throws();
} catch (e) {
console.log('asynchronously handled', e.message);
}
try {
await rejects();
} catch (e) {
console.log('asynchronously handled', e.message);
}
}());
A critical point to keep in mind is that asynchronous errors will not be caught if you forget to await
the promise that rejects with them. This is the analog of forgetting to return
inside of .then
or a .catch
callback
You could just throw and exception if your value turns up empty.
export const getById = async (id) => {
const user = await users.findOne({ where: {
id: id
}});
if (!user) {
throw Error('User does not exist..');
}
return user;
}
本文标签: javascriptasync and await and promise in regards to catching errorsStack Overflow
版权声明:本文标题:javascript - async and await and promise in regards to catching errors - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742349523a2458204.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论