javascript - Is it a bad practice to use await inside a while loop - Stack Overflow

Is it a bad practice to use a await inside a while loop?I have the following code: this is inside a a

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:

  1. Is it ok to use two res variables?
  2. Am I going to use performance because I'm using a await inside a while loop?
  3. 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:

  1. Is it ok to use two res variables?
  2. Am I going to use performance because I'm using a await inside a while loop?
  3. 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 a ReferenceError because of the temporal dead zone. Drop the let. – Bergi Commented May 9, 2018 at 12:39
 |  Show 2 more ments

1 Answer 1

Reset to default 5

Is 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信