Discuss / JavaScript / 测试异步变同步操作未成功(前端菜鸡,有错望见谅,帮我指出下,感谢)

测试异步变同步操作未成功(前端菜鸡,有错望见谅,帮我指出下,感谢)

Topic source

function test0() {

setTimeout(() => {

// test1();

console.log(0);

}, 3000);

}

function test1() {

setTimeout(() => {

// test2();

console.log(1);

}, 2000);

}

function test2() {

setTimeout(() => {

console.log(2);

return 2;

}, 1000);

}

// test0();

function* test() {

var r1 = yield test0();

var r2 = yield test1();

var r3 = yield test2();

console.log(r3);

}

let g = test();

g.next();

g.next();

g.next();

g.next();

测试结果: 

undefined

 2

1

0

与期望不符,有点不解,是不是我的测试方法有问题,望解答


  • 1

Reply