1
Sparetire 2018-08-11 01:05:03 +08:00 via Android
async/await
|
2
qshu 2018-08-11 08:47:11 +08:00
var[n].write
|
3
947211232 2018-08-11 08:50:29 +08:00
递归
|
4
SakuraKuma 2018-08-11 09:40:18 +08:00 1
let
vars = [var1, var2, var3, etc...], latency = 1e3, run = () => { let cVar = vars.shift(); cVar.write({ onComplete: setTimeout(run, latency) }) }; setTimeout(run, latency); 大概这种感觉?? |
5
y051313 OP @SakuraKuma 多谢
|
6
e8c47a0d 2018-08-14 09:48:28 +08:00
let wait = duration => new Promise(resolve => setTimeout(resolve, duration))
let vs = [{}, {}, {}, ...] // 把这里的 {} 换成有 write 的对象 async function loop () { for (let i = 0; i < vs.length; i++) { // 不要用 forEach let v = vs[i] await v.write() await wait(1000) } } loop() |
7
e8c47a0d 2018-08-14 11:49:26 +08:00
v = {
async write () { try { // 在这里做处理 return true // 这个 return 可以缺省 } catch (e) { throw e } } } |