timeout vs promise timeout in ev

2018-07-30  本文已影响10人  超薄智能
console.log('---start---');

const timeOut = (val,wait)=>{
  setTimeout(()=>{
    console.log(val?val:"timeout");
  },(wait?wait:0));
}

const fPromise = (parm)=>{
    return new Promise((resolve,reject)=>{
        resolve(parm);
    });
}

fPromise('Ptime0')
  .then(val=>timeOut(val,0));

timeOut('time0',0);
timeOut('time1',1);
timeOut('time1.9',1.9);
timeOut('time2',2);

my excepted result

---start---
time0
Ptime0
time1 
time1.9
time2

· result in my browser is bellow, and the result seems steady, but few times Ptime0 will become the last one.
· 2 millisecond is like the margin time for my computer to transfer the timeout from Micro Task Queue (the promise in) to the Task Queue
· but why the task in task Queue executed before the transfer process is done!! how could it be if is performed by sequence, and how could it be parallel in a single thread with event loop.

---start---
time0
time1
time1.9
Ptime0
time2
上一篇 下一篇

猜你喜欢

热点阅读