最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - async and await and promise in regards to catching errors - Stack Overflow

programmeradmin0浏览0评论

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 example undefined. Secondly, the try/catch is unnecessary and can go away. If user 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
Add a ment  | 

3 Answers 3

Reset to default 7

First, 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;        
}
发布评论

评论列表(0)

  1. 暂无评论