[英]Throw Error with loop of await function
I'm struggling with a loop of async await function. 我正在努力与异步await函数循环。 I'm creating an API which POST a request to another API.
我正在创建一个将请求发布到另一个API的API。 I have to make a loop because users could insert many items at once in the database.
我必须进行循环,因为用户可以一次在数据库中插入许多项目。
The API that I call rejects Parallel executions so I can't use Promise.all
:/ 我调用的API拒绝并行执行,所以我不能使用
Promise.all
:/
In order to implement that service I made a function to insert one object : 为了实现该服务,我制作了一个函数来插入一个对象:
const addValue = async (ID:number, cookie: string) => {
try {
const addValueBody = await addValueQuery(ID:number)
const header = { 'cookie': cookie, 'content-type': 'application/json' }
const fetchResult = fetch('http://api-to-call.xyz', { method: `POST`, headers: header, body: addValueBody })
const response = await fetchResult
const jsonData = await response.json()
return jsonData
} catch (e) {
throw Error(e.message)
}
}
And a function which will execute addValue
inside a for loop : 还有一个将在for循环内执行
addValue
的函数:
const addMulti = async (values: any, cookie: string) => {
for (const item of values) {
await addValue(item.ID, cookie)
}
}
The problem is that when I call addMulti and if there is an Error I have a UnhandledPromiseRejectionWarning
. 问题是,当我调用addMulti时,如果出现错误,则有
UnhandledPromiseRejectionWarning
。
Also I tried to put de for ... in
or await addValue
inside a try catch
but it doesn't catch the Error(s) 我也尝试将de
for ... in
或在try catch
await addValue
但没有捕获到错误
Hence, I have 2 questions : 因此,我有两个问题:
How can I throw en Error if it occurs during the execution of the loop ? 如果在循环执行期间发生错误,如何抛出en错误? Is it possible to break the loop if there is an error ?
如果有错误,是否可以中断循环?
Thanks for your help :) 谢谢你的帮助 :)
How can I throw en Error if it occurs during the execution of the loop ?
如果在循环执行期间发生错误,如何抛出en错误? Is it possible to break the loop if there is an error ?
如果有错误,是否可以中断循环?
Your code does that already, by await
ing addValue
. 您的代码已经通过
await
addValue
。 If addValue
throws an error, since addMulti
doesn't catch
, it terminates addMulti
as well. 如果
addValue
引发错误,因为addMulti
不能catch
,它addMulti
终止addMulti
。
The problem is that when I call addMulti and if there is an Error I have a UnhandledPromiseRejectionWarning.
问题是,当我调用addMulti时,如果出现错误,则显示UnhandledPromiseRejectionWarning。
The code you've shown is correct¹, the problem is that it appears you have no error handling on the call to addMulti
. 您显示的代码是正确的¹,问题在于,看来对
addMulti
的调用没有错误处理。 That code must either: 该代码必须:
try
/ catch
in an async
function if you don't want to allow it to propagate, or via the catch
method if using the promise directly), or try
/ catch
async
函数,如果直接使用promise,则可以通过catch
方法),或者 async
function, or by returning the promise in a non- async
function) async
函数中捕获错误,或通过在非async
函数中返回promise) So for instance, if the call to addMulti
is in a non- async
function and if nothing calling that function will handle errors, you need to handle it at that stage: 因此,例如,如果对
addMulti
的调用是在非async
函数中,并且没有任何调用该函数的函数会处理错误,则需要在该阶段处理它:
addMulti(/*...*/)
.catch(error => {
// ...handle/report the error
});
If it's in an async
function but nothing handles that async
function's errors, then: 如果它在
async
函数中,但没有任何东西可以处理该async
函数的错误,则:
try {
await addMulti(/*...*/);
} catch (e) {
// ...handle/report the error
}
¹ ...other than it seems odd to catch e
and then do throw Error(e.message)
; ¹...除了抓住
e
然后throw Error(e.message)
似乎很奇怪; just let the error propagate. 只是让错误传播。
When you get UnhandledPromiseRejection
working with async functions, it almost surely means that you didn't catch the thrown error. 当您使
UnhandledPromiseRejection
与异步函数一起使用时,几乎可以肯定地意味着您没有捕获引发的错误。 In your case you could just wrap the whole loop in try{...} catch{...}
block: 在您的情况下,您可以将整个循环包装在
try{...} catch{...}
块中:
const addMulti = async (values: any, cookie: string) => {
try {
for (const item of values) {
await addValue(item.ID, cookie)
}
} catch(e) {
console.log('Error occured in async function')
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.