2017-08-17 48 views
3

我一直试图围绕我的问题,但没有成功,因此我要求你的帮助。异步代码执行说明

让我们看看下面的例子:

const async = require('async') 
var counter = 0 

var test = cb => { 
    if (++counter < 3) { 
    setTimeout(() => { 
     async.each([0,1,2], (item, callback) => { 
     console.log(item) 
     console.log('counter inner ' + counter) 
     test(cb) 
     callback() 
     },() => { 
      cb() 
     }) 
    }) 
    } else { 
     console.log('counter ' + counter) 
    } 
} 

var prom = new Promise((res, rej) => test(() => res('done'))) 
prom.then(res => console.log(res)) 

输出是:

0 
counter inner 1 
1 
counter inner 2 
counter 3 
2 
counter inner 3 
counter 4 
done 
0 
counter inner 4 
counter 5 
1 
counter inner 5 
counter 6 
2 
counter inner 6 
counter 7 

我不明白的是它是如何在第二0后立即打印counter inner 4。不应该打印counter inner 1?一个承诺只能解决一次?第二次解决呼叫会发生什么?谢谢。

+0

可能的重复[为什么我的变量在函数内部修改后没有改变? - 异步代码引用](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Liam

+0

* [One单词答案:异步性。](https://stackoverflow.com/a/23667087/542251)* – Liam

回答

0

有点难以一步一步解释它,但我会尝试向后 - 从输出的意义来说,我会尝试提及代码中的哪一行输出它以及何时输出。

// obviously starting inside the callback async runs for each of your 3 items 

    0 // console.log(item); 
    counter inner 1 // console.log('counter inner ' + counter); 

    1 // console.log(item); 
    counter inner 2 // console.log('counter inner ' + counter); 
    counter 3 // let's not forget you also call callback() for each item 
      // which evaluates this condition if (++counter < 3) and starting from here, 
      // it will be true and will run your else statement 
      // console.log('counter ' + counter); 

    2 // console.log(item); 
    counter inner 3 // console.log('counter inner ' + counter); 
    counter 4 // console.log('counter ' + counter); 

// at this point your promise is already resolved (and yes, only once) 
    done // prom.then(res => console.log(res)) 

// Now you are probably questioning where the following output comes from 
// remember this condition? if (++counter < 3) 
// before starting to evaluate to false and printing to console, once it 
// evaluated to true so setTimeout was called with your async callback 
// which scheduled it to run on the next "tick"*, which is right after the 
// promised was resolved 

    0 
    counter inner 4 
    counter 5 
    1 
    counter inner 5 
    counter 6 
    2 
    counter inner 6 
    counter 7 

这是setTimeout()的超时时间为0的效果。它就像C中的线程/进程产量。虽然它似乎说“立即运行”,但它实际上会在执行队列末尾重新排队新的javaScript代码。

根据setTimeout文档,它的第二个参数是以下各项:

的时间,以毫秒为单位(千分之一秒),定时器应该 指定功能之前等待或执行的代码。如果省略参数 ,则使用值0,意味着立即执行 或更准确地尽快执行。请注意,在任一情况下,实际延迟可能比预期的要长;

在这里你可以阅读更多关于reasons for delays longer than specified

您可以通过将if条件更改为if(++counter < 2)来轻松验证我刚刚解释的内容。由于它永远不会评估为真,您将看到控制台输出停止在"done"