为什么在while表达式中使用await的赋值不能保留赋值? [英] Why does assignment using await within while expression not retain assigned value?

查看:123
本文介绍了为什么在while表达式中使用await的赋值不能保留赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出JavaScript代码

Given the JavaScript code

function* gen(start = 0, stop = 5) {
   while (start < stop) yield ++start;
}

async function fn(g = gen()) {
  while (done = !await new Promise(resolve => 
                   setTimeout(resolve, 1000, g.next()))
                   .then(({value, done}) => {
                     // `value:undefined, done:true` following
                     // `value:5, done:false`
                     console.log(`value:${value}, done:${done}`); 
                     return done 
                   }
                   , err => Promise.reject(err)));
  return done; // what happened to the `true` value assigned?
}
// why is `res` `false`?
fn().then(res => console.log(`res:${res}`), err => console.error(err));

fn()返回的done的预期结果是true,当在while表达式中为done分配了从.then()返回的值true时.

the expected result of done returned from fn() is true when done is assigned the value true that is returned from .then() within while expression.

为什么使用 await while 语句

Why is the assignment using await at while statement expression not retained?

推荐答案

JavaScript具有三种"不同类型的范围:本地,全局和块.

JavaScript has "three" different types of scope: local, global, and block.

全局

这种类型的变量可以在代码的所有位置出现:

This type of variable can be reach in all places of the code:

var b = "bar";
   (function test1(a) {
         if (a == true) {
              var b = "bar";
         } else {
              var b = "foo";
         }
         return b;
    })(true);
    alert(b);

本地

只有在代码的一般范围内的变量时,才能访问这些变量:

These variables can only be reached if the variable in the general scope of the code:

(function test2(a) {
     if (a == true) {
          var b = "bar";
     } else {
          var b = "foo";
     }
     return b;
})(true)

在这种情况下,一般范围是功能.即使变量b受if语句限制,该函数仍然可以到达它.

In this case, the general scope is the function. Even though the variable b is block scoped by the if statement, the function can still reach it.

阻止

自定义的块范围变量对于JavaScript来说还很陌生,可以使用let关键字引入:

Self defined block scoped variables are pretty new to JavaScript and can be introduced using the let keyword:

(function test2(a) {
     if (a == true) {
          let b = "bar";
     } else {
          let b = "foo";
     }
     return b;
})(true)

这将导致错误,因为变量b现在被限制为if状态.因此,在您的代码中,我们可以更改范围,以允许您更改完成,在异步循环的情况下,while循环的作用域是块作用域.

This will result in an error because the variable b is now block scoped to the if state. Thus in your code, we can change your scoping to allow you to change done which in a while loop is block scoped in the case of asynchronous loops.

function* gen(start = 0, stop = 5) {
   while (start < stop) yield ++start;
}

async function fn(g = gen()) {
  let doneVal;
  while (done = !await new Promise(resolve => 
                   setTimeout(resolve, 1000, g.next()))
                   .then(({value, done}) => {
                     // `value:undefined, done:true` following
                     // `value:5, done:false`
                     console.log(`value:${value}, done:${done}`); 
                     return done 
                   }
                   , err => Promise.reject(err))) {
       doneVal = done;
  }

  return doneVal; // what happened to the `true` value assigned?
}
// why is `res` `false`?
fn().then(res => console.log(`res:${res}`), err => console.error(err));

现在您将得到res:true.在您的特定示例中,这样的代码存在问题:

Now you will get res:true. In your specific example, there is an issue with code like this:

var i;
var c = 0;
while (i = 90 && c < 10) {
    c++;
}
console.log(i, c);

i为false,而c等于10

i is false while c is equal to 10

这篇关于为什么在while表达式中使用await的赋值不能保留赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆