美文网首页
timeout vs promise timeout in ev

timeout vs promise timeout in ev

作者: 超薄智能 | 来源:发表于2018-07-30 23:01 被阅读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

相关文章

网友评论

      本文标题:timeout vs promise timeout in ev

      本文链接:https://www.haomeiwen.com/subject/mbiqvftx.html