// asiox 不支持jsonp，设置6000ms 超时
//https://api.asilu.com/idcard/ 可用该地址进行jsonp测试

export default function jsonp(url) {
  function clean(script, cb) {
    Promise.resolve().then(() => {
      if (script) {
        document.body.removeChild(script)
        delete window[cb]
      }
    })
  }
  return new Promise((rs, rj) => {
    const script = document.createElement('script')
    const cb = `cb_${Date.now()}`
    script.src = `${url}?callback=${cb}`
    document.body.append(script)
    setTimeout(clean, 6000)
    window[cb] = function (data) {
      rs(data)
      clean(script, cb)
    }
    script.onerror = () => {
      rj()
      clean(script, cb)
    }
    setTimeout(clean, 6000)
  })
}