鍍金池/ 問(wèn)答/HTML/ Js鏈?zhǔn)秸{(diào)用如何加條件?

Js鏈?zhǔn)秸{(diào)用如何加條件?

我想用 userProxy 判斷是否加上.proxy(),應(yīng)該怎么樣優(yōu)雅實(shí)現(xiàn)呢。

if (useProxy) {
  data = await superAgent
    .get(send.url)
    .proxy(proxy)
    .query(send.query)
    .timeout({
      response: 8000
    })
    .set({
      'Content-Type': 'application/x-www-form-urlencoded'
    })
} else {
  data = await superAgent
    .get(send.url)
    .query(send.query)
    .timeout({
      response: 15000
    })
    .set({
      'Content-Type': 'application/x-www-form-urlencoded'
    })
}
回答
編輯回答
憶當(dāng)年

運(yùn)用三目運(yùn)算符

2018年7月17日 23:37
編輯回答
安于心
let agent = await superAgent.get(send.url)
if(useProxy){
    agent = await agent.proxy(proxy)
}
data = await agent.query(send.query)
    .timeout({
      response: 8000
    })
    .set({
      'Content-Type': 'application/x-www-form-urlencoded'
    })

這樣應(yīng)該可以吧,不太確定哪里有異步,都加個(gè)await總沒(méi)錯(cuò)

2017年3月2日 17:28