Javascript - 局部对象变量在 promise 和 await 之间不守恒
Javascript - Local object variable not conserved between promise and await
我正在使用下面的代码,它强调了一个事实,即局部对象变量 (HitCurrent
) 在 computeHit(HitCurrent,'computer')
的核心之间和以下列方式调用此函数之后不守恒: await computeHit(HitCurrent, 'computer');
在computeHit(HitCurrent,'computer')
的核心中,数组HitCurrent.arrayCurrent
被修改了(实际上,它计算了计算机的命中):但问题是一旦我回到主目录,修改就不会被保留线程(在 await computeHit(HitCurrent, 'computer')
.
之后
如果我这样做,在 await
之后,此局部对象变量中包含的一个数组的 console.log
,我得到的数组与正确计算到核心的数组不同computeHit
函数。我不明白这种行为。
// Perform hit computer
(async () => {
// Wait computeHit function
await computeHit(HitCurrent, 'computer');
// Output array which is not the same than into computeHit
console.log('into async : HitCurrent.arrayCurrent', HitCurrent.arrayCurrent);
alert('into async() function');
})();
}
与 computerHit
功能类似:
function computeHit(HitCurrent, mode) {
if (mode == 'computer') {
return new Promise( resolve => {
// Creation of webworker
let firstWorker = new Worker(workerScript);
firstWorker.onmessage = function (event) {
resolve(event.data);
}
// Post current copy of HitCurrent, i.e HitCurrent
firstWorker.postMessage([HitCurrent, HitCurrent.playerCurrent, maxNodes]);
}).then(({result}) => {
// Get back game board of webworker
HitCurrent = result.HitResult;
// Get back suggested hit computed by webworker
[a,b] = HitCurrent.coordPlayable;
// Drawing all lines from suggested hit (in 8 directions)
// HitCurrent.arrayCurrent is modified HERE !
for (k = 0; k < 8; k++) {
exploreHitLine(HitCurrent, a, b, k, 'drawing');
}
// Remove playable hits
cleanHits('playable', HitCurrent);
// Display current game
displayCurrentHit(HitCurrent);
// Output array which is good
alert('into computeHit function');
console.log('into promise - HitCurrent.arrayCurrent', HitCurrent.arrayCurrent);
})
}
}
我希望在将它计算到 computeHit
函数和 await
调用之后返回相同的数组(即 HitCurrent.arrayCurrent
),但事实并非如此。
我该如何解决这个问题?我试图传递全局变量,但没有成功。如果需要,请随时向我询问更多信息。
在函数内分配参数对在调用中用作该参数参数的任何变量没有任何影响。简化您的代码:
function computeHit(HitCurrent) {
HitCurrent = "something else";
}
let HitCurrent = "something";
computeHit(HitCurrent);
console.log(HitCurrent); // "something", not "something else"
computeHit(HitCurrent)
中的 HitCurrent
变量和 computeHit
调用中的 HitCurrent
参数之间没有任何 link,除了第一个的值被读取并作为第二个的值传递给函数。
如果您想根据 computeHit
中发生的情况更新 HitCurrent
,则:
Return 新的 HitCurrent
并将其赋值回变量:HitCurrent = computeHit(...
使 HitCurrent
引用一个对象,并修改对象的状态(例如,在其上使用属性)。
让我们重写您的代码 只是 一点并重命名一些变量。
从字面上看,我已将 computeHit(..)
的参数重命名为 my_variable_1
和 my_variable_2
。
您可能希望 check/admit 这不会更改代码执行。
您现在应该明白为什么您的异步代码块中的 HitCurrent
从未被修改过。请参阅简化代码块 3,将 产生 42。希望对您有所帮助。
代码块 1 不变:
// Perform hit computer
(async () => {
// Wait computeHit function
await computeHit(HitCurrent, 'computer');
// Output array which is not the same than into computeHit
console.log('into async : HitCurrent.arrayCurrent', HitCurrent.arrayCurrent);
alert('into async() function');
})();
具有重命名变量的代码块 2:
function computeHit(my_variable_1, my_variable_2) {
if (my_variable_2 == 'computer') {
return new Promise( resolve => {
// Creation of webworker
let firstWorker = new Worker(workerScript);
firstWorker.onmessage = function (event) {
resolve(event.data);
}
// Post current copy of my_variable_1, i.e my_variable_1
firstWorker.postMessage([my_variable_1, my_variable_1.playerCurrent, maxNodes]);
}).then(({result}) => {
// Get back game board of webworker
my_variable_1 = result.HitResult;
// Get back suggested hit computed by webworker
[a,b] = my_variable_1.coordPlayable;
// Drawing all lines from suggested hit (in 8 directions)
// my_variable_1.arrayCurrent is modified HERE !
for (k = 0; k < 8; k++) {
exploreHitLine(my_variable_1, a, b, k, 'drawing');
}
// Remove playable hits
cleanHits('playable', my_variable_1);
// Display current game
displayCurrentHit(my_variable_1);
// Output array which is good
alert('into computeHit function');
console.log('into promise - my_variable_1.arrayCurrent', my_variable_1.arrayCurrent);
})
}
}
代码块 3,简化:
// Perform hit computer
var magicNumber = 42;
(async () => {
await computeHit(magicNumber, 'computer');
console.log('magic is ', magicNumber);
})();
function computeHit(my_variable_1, my_variable_2) {
if (my_variable_2 == 'computer') {
// Get back game board of webworker
my_variable_1 = 314159;
}
}
我正在使用下面的代码,它强调了一个事实,即局部对象变量 (HitCurrent
) 在 computeHit(HitCurrent,'computer')
的核心之间和以下列方式调用此函数之后不守恒: await computeHit(HitCurrent, 'computer');
在computeHit(HitCurrent,'computer')
的核心中,数组HitCurrent.arrayCurrent
被修改了(实际上,它计算了计算机的命中):但问题是一旦我回到主目录,修改就不会被保留线程(在 await computeHit(HitCurrent, 'computer')
.
如果我这样做,在 await
之后,此局部对象变量中包含的一个数组的 console.log
,我得到的数组与正确计算到核心的数组不同computeHit
函数。我不明白这种行为。
// Perform hit computer
(async () => {
// Wait computeHit function
await computeHit(HitCurrent, 'computer');
// Output array which is not the same than into computeHit
console.log('into async : HitCurrent.arrayCurrent', HitCurrent.arrayCurrent);
alert('into async() function');
})();
}
与 computerHit
功能类似:
function computeHit(HitCurrent, mode) {
if (mode == 'computer') {
return new Promise( resolve => {
// Creation of webworker
let firstWorker = new Worker(workerScript);
firstWorker.onmessage = function (event) {
resolve(event.data);
}
// Post current copy of HitCurrent, i.e HitCurrent
firstWorker.postMessage([HitCurrent, HitCurrent.playerCurrent, maxNodes]);
}).then(({result}) => {
// Get back game board of webworker
HitCurrent = result.HitResult;
// Get back suggested hit computed by webworker
[a,b] = HitCurrent.coordPlayable;
// Drawing all lines from suggested hit (in 8 directions)
// HitCurrent.arrayCurrent is modified HERE !
for (k = 0; k < 8; k++) {
exploreHitLine(HitCurrent, a, b, k, 'drawing');
}
// Remove playable hits
cleanHits('playable', HitCurrent);
// Display current game
displayCurrentHit(HitCurrent);
// Output array which is good
alert('into computeHit function');
console.log('into promise - HitCurrent.arrayCurrent', HitCurrent.arrayCurrent);
})
}
}
我希望在将它计算到 computeHit
函数和 await
调用之后返回相同的数组(即 HitCurrent.arrayCurrent
),但事实并非如此。
我该如何解决这个问题?我试图传递全局变量,但没有成功。如果需要,请随时向我询问更多信息。
在函数内分配参数对在调用中用作该参数参数的任何变量没有任何影响。简化您的代码:
function computeHit(HitCurrent) {
HitCurrent = "something else";
}
let HitCurrent = "something";
computeHit(HitCurrent);
console.log(HitCurrent); // "something", not "something else"
computeHit(HitCurrent)
中的 HitCurrent
变量和 computeHit
调用中的 HitCurrent
参数之间没有任何 link,除了第一个的值被读取并作为第二个的值传递给函数。
如果您想根据 computeHit
中发生的情况更新 HitCurrent
,则:
Return 新的
HitCurrent
并将其赋值回变量:HitCurrent = computeHit(...
使
HitCurrent
引用一个对象,并修改对象的状态(例如,在其上使用属性)。
让我们重写您的代码 只是 一点并重命名一些变量。
从字面上看,我已将 computeHit(..)
的参数重命名为 my_variable_1
和 my_variable_2
。
您可能希望 check/admit 这不会更改代码执行。
您现在应该明白为什么您的异步代码块中的 HitCurrent
从未被修改过。请参阅简化代码块 3,将 产生 42。希望对您有所帮助。
代码块 1 不变:
// Perform hit computer
(async () => {
// Wait computeHit function
await computeHit(HitCurrent, 'computer');
// Output array which is not the same than into computeHit
console.log('into async : HitCurrent.arrayCurrent', HitCurrent.arrayCurrent);
alert('into async() function');
})();
具有重命名变量的代码块 2:
function computeHit(my_variable_1, my_variable_2) {
if (my_variable_2 == 'computer') {
return new Promise( resolve => {
// Creation of webworker
let firstWorker = new Worker(workerScript);
firstWorker.onmessage = function (event) {
resolve(event.data);
}
// Post current copy of my_variable_1, i.e my_variable_1
firstWorker.postMessage([my_variable_1, my_variable_1.playerCurrent, maxNodes]);
}).then(({result}) => {
// Get back game board of webworker
my_variable_1 = result.HitResult;
// Get back suggested hit computed by webworker
[a,b] = my_variable_1.coordPlayable;
// Drawing all lines from suggested hit (in 8 directions)
// my_variable_1.arrayCurrent is modified HERE !
for (k = 0; k < 8; k++) {
exploreHitLine(my_variable_1, a, b, k, 'drawing');
}
// Remove playable hits
cleanHits('playable', my_variable_1);
// Display current game
displayCurrentHit(my_variable_1);
// Output array which is good
alert('into computeHit function');
console.log('into promise - my_variable_1.arrayCurrent', my_variable_1.arrayCurrent);
})
}
}
代码块 3,简化:
// Perform hit computer
var magicNumber = 42;
(async () => {
await computeHit(magicNumber, 'computer');
console.log('magic is ', magicNumber);
})();
function computeHit(my_variable_1, my_variable_2) {
if (my_variable_2 == 'computer') {
// Get back game board of webworker
my_variable_1 = 314159;
}
}