Async await 处理链接函数async/await try/catch时出错
我正在看medim上发布的示例。我正在努力学习更好的错误处理。try/catch似乎是使用async/await处理错误的方法。第二个函数依赖于第一个函数返回的id。但在这种情况下,您将如何使用try/catchAsync await 处理链接函数async/await try/catch时出错,async-await,Async Await,我正在看medim上发布的示例。我正在努力学习更好的错误处理。try/catch似乎是使用async/await处理错误的方法。第二个函数依赖于第一个函数返回的id。但在这种情况下,您将如何使用try/catch let sendData = async() => { let somePhotos = this.somePhotos let sendInfo = await axios.post("https://yourapihere.com/path&qu
let sendData = async() => {
let somePhotos = this.somePhotos
let sendInfo = await axios.post("https://yourapihere.com/path", {
yourName: "Mehmet",
yourAge: 29,
yourTitle: "Software Developer"
})
let id = sendInfo.data
let sendPhotos = await axios.post("https://yoursecondapihere.com/path", {
id: id,
photos: somePhotos
})
}
我认为这是对整个事情使用try/catch的正确方法。但是如果你想通知第一篇帖子出错了怎么办
let sendData = async() => {
let somePhotos = this.somePhotos
try {
let sendInfo = await axios.post("https://yourapihere.com/path", {
yourName: "Mehmet",
yourAge: 29,
yourTitle: "Software Developer"
})
let id = sendInfo.data
let sendPhotos = await axios.post("https://yoursecondapihere.com/path", {
id: id,
photos: somePhotos
})
} catch (error) {
console.error(error);
}
}
这是正确的吗?这会停止第二个要执行的函数吗?
让sendData=async()=>{
让somePhotos=this.somePhotos
try {
let sendInfo = await axios.post("https://yourapihere.com/path", {
yourName: "Mehmet",
yourAge: 29,
yourTitle: "Software Developer"
})
if (!sendInfo ) {
throw 'Failed sendInfo';
}
let id = sendInfo.data
let sendPhotos = await axios.post("https://yoursecondapihere.com/path", {
id: id,
photos: somePhotos
})
} catch (error) {
console.error(error);
}
}
它会在某个东西抛出异常后立即停止。只要在调用
sendData
函数时调用try/catch
。