Last active
December 8, 2021 20:27
-
-
Save sanslan/ffbac535a21797ad962628034a035790 to your computer and use it in GitHub Desktop.
multiple catch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const app = express() | |
const port = 3000 | |
const axios = require('axios') | |
async function get_users1(){ | |
let users =await axios.get('https://jsonplaceholder.typicode.com/users') | |
return users.data | |
} | |
async function get_users2(){ | |
let users =await axios.get('https://jsonplaceholder.typicode.com/users2') | |
return users.data | |
} | |
async function exception_handler(fn,error_callback){ | |
try{ | |
return await fn() | |
}catch{ | |
error_callback() | |
} | |
} | |
app.get('/', async(req, res) => { | |
let users1 =await exception_handler(get_users1,() => res.json({'error': 'error in get_users1'})) | |
let users2 =await exception_handler(get_users2,() => res.json({'error': 'error in get_users2'})) | |
res.json({'users1': users1,'users2': users2}) | |
}) | |
app.listen(port, () => { | |
console.log(`Example app listening at http://localhost:${port}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment