zhouzhongyuan / qa

Questions recods
MIT License
5 stars 1 forks source link

异步是什么? #46

Open zhouzhongyuan opened 7 years ago

zhouzhongyuan commented 7 years ago

文章

zhouzhongyuan commented 7 years ago

竞态条件(race condition):函数顺序的不确定性。

foo与bar相互竞争,看谁先运行。因为无法可靠预测a和b的最终结果,所以才叫做race condition.

var a = 1;
var b = 2;

function foo() {
    a++;
    b = b * a;
    a = b + 3;
}

function bar() {
    b--;
    a = 8 + b;
    b = a * 2;
}

// ajax(..) is some arbitrary Ajax function given by a library
ajax( "http://some.url.1", foo );
ajax( "http://some.url.2", bar );
zhouzhongyuan commented 7 years ago

foo和bar中的代码具有原子性。 有些函数不具有run-to-completion的特性,那么最终结果就会有很多。ES6引入了这个东西。 //TODO 什么东西? 答:生成器。generator函数。

zhouzhongyuan commented 7 years ago
zhouzhongyuan commented 5 years ago