Promise与setTimeout的错误示范
错误写法
export const checkSDK = () => { var timer = null; const start = Date.now(); return new Promise((resolve, reject) => { if (typeof window.TigerBridge === 'object') { resolve(); return; } if (Date.now() - start <= 5 * 1000) { clearTimeout(timer); timer = setTimeout(checkSDK, 100); return; } reject(); }) }
正确写法
export const checkBridge = () => { var timer = null; const start = Date.now(); function check(resolve, reject) { if (typeof window.TigerBridge === 'object') { resolve(); return true; } if (Date.now() - start <= 5 * 1000) { clearTimeout(timer); timer = setTimeout(check.bind(null, resolve, reject), 100); return; } reject(); return false; } return new Promise((resolve, reject) => check(resolve, reject)) }
模仿SDK注入过程,
if (process.env.NODE_ENV != 'production') { setTimeout(() => { window.TigerBridge = { getAccessToken: () => { return pkg.token; }, isAccountPermissionLimited: () => false }; }, 1600); }
相关推荐
89500297 2020-05-05
sfkong 2020-08-02
delmarks 2020-04-09
gjcxywwx 2019-11-04
前端开发Kingcean 2019-10-22
Magicsoftware 2019-10-19
xuewenke 2012-02-05
jiangbo0 2019-09-08
85403263 2019-07-01
codable 2019-07-01
梦秋雨 2019-07-01
87241940 2019-06-30
83251242 2019-06-30
85281643 2019-06-29
whynotgonow 2019-06-29