Get data using await async without try catch

后端 未结 5 1815
抹茶落季
抹茶落季 2020-12-15 02:11

I am trying to use await-async without try-catch for this:

<
5条回答
  •  鱼传尺愫
    2020-12-15 02:50

    If you have a valid default for the error case you can use the catch method on the getUsers promise and then await a promise whose error will be handled

    const users = async () => {
        const value = await getUsers(1000, false).catch(e => null);
    }
    

    While this approach should work it should be noted that this may mask the case when getUsers returns null vs when it raises an error, and you will still need to check for the null or get a null access error. All in all I would stick with the try { .. } catch (e) { ... } for most casses

提交回复
热议问题