Is it a bad practice to use a await inside a while loop?
I have the following code:
// this is inside a async function
try {
let res = await api.resource.create(...args) // create resource
do {
let res = await api.resource.show(res.body.id)
if (res.body.status === 'COMPLETED')
return res.body
} while(1)
} catch (err) {
errorHandler(err)
}
I have a couple of questions here:
- Is it ok to use two res variables?
- Am I going to use performance because I'm using a await inside a while loop?
- Is there a better solutin?
Thank you in advance.
Is it a bad practice to use a await inside a while loop?
I have the following code:
// this is inside a async function
try {
let res = await api.resource.create(...args) // create resource
do {
let res = await api.resource.show(res.body.id)
if (res.body.status === 'COMPLETED')
return res.body
} while(1)
} catch (err) {
errorHandler(err)
}
I have a couple of questions here:
- Is it ok to use two res variables?
- Am I going to use performance because I'm using a await inside a while loop?
- Is there a better solutin?
Thank you in advance.
Share asked May 9, 2018 at 11:56 FXuxFXux 4357 silver badges16 bronze badges 7- 2 Key question to answer: why are you using a while loop here? – Adam Jenkins Commented May 9, 2018 at 11:58
-
Hmm kind off.. This basically renders the benefit of using a async method pletely useless, since you could just synchronously do this. Also what is the purpose of having a variable
res
in the try block and in the while loop. – Bellian Commented May 9, 2018 at 12:19 -
For 1, it makes your code less readable. For 2, pared to what alternative? For 3, depends on 2, but you might want to add a timeout to not overkill your server (if
api.resource.show()
indeed includes a call to a server). Otherwise, in a general sense, await in while loop is just fine. – Kaiido Commented May 9, 2018 at 12:26 -
@Kaiido In fact the timeout is not needed IF
api.resource.show()
includes some sort of async call, since the await will stop execution until the returned Promise resolves ;) – Bellian Commented May 9, 2018 at 12:30 -
1
Just saying,
let res = …(res.body.id)
in a block scope will always throw aReferenceError
because of the temporal dead zone. Drop thelet
. – Bergi Commented May 9, 2018 at 12:39
1 Answer
Reset to default 5Is it ok to use two
res
variables?
No. The way you are using the inner one, the access in the arguments is always in the temporal dead zone and you will always get an exception.
Am I going to use performance because I'm using a
await
inside a while loop?
There's nothing wrong with using await
in loops, as long as you expect them to run sequentially and not all iterations concurrently.
Is there a better solution?
try {
let res = await api.resource.create(...args) // create resource
do {
res = await api.resource.show(res.body.id)
} while (res.body.status !== 'COMPLETED')
return res.body
} catch (err) {
errorHandler(err)
}
Notice also that if errorHandler
es from a callback parameter, you should drop the entire try
/catch
and just use .catch(errorHandler)
on the promise returned by the call.
Also, while I don't know what api.resource.show
does, it looks like you are polling for a result. It would be better if the method would just return a promise that fulfills at the right time. If that is not possible and you need to poll, I would remend at least some delay between the calls.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745045589a4608077.html
评论列表(0条)