Return 从 promise 到 async() 块的修改对象
Return a modfied object from a promise to async() block
我启动了一个 webworker(计算游戏中的命中)到一个 promise 中,我想在 webworker 完成计算后取回结果(总是一个数组)。
我尝试在 promise 结束时 return then(
之后的对象并在主线程中执行:
(async () => {
// Wait computeHit function
await computeHit(HitCurrent, 'computer');
但似乎当 computeHit
完成的计算很高时,promise
中的 return HitCurrent
和 (async () =>
块中的 HitCurrent = await
选项之间存在冲突.
看下面的代码就更清楚了:
背景包括:
1) webworker的使用
2) promise的使用
3) async/await 关键字
承诺块:
function computeHit(HitCurrent, mode) {
if (mode == 'computer') {
let HitTemp = JSON.parse(JSON.stringify(HitCurrent));
return new Promise( resolve => {
// Creation of webworker
firstWorker = new Worker(workerScript);
firstWorker.onmessage = function (event) {
resolve(event.data);
}
// Post current copy of HitCurrent, i.e HitCurrent
firstWorker.postMessage([HitTemp, HitTemp.playerCurrent, maxNodes]);
}).then(({result}) => {
// Get back game board of webworker
HitTemp = result.HitResult;
// Get back suggested hit computed by webworker
[a,b] = HitTemp.coordPlayable;
// Drawing all lines from suggested hit (in 8 directions)
// HERE, we modify HitCurrent attributes (like array)
for (k = 0; k < 8; k++) {
exploreHitLine(HitCurrent, a, b, k, 'drawing');
}
// Remove playable hits
cleanHits('playable', HitCurrent);
// Display current game
displayCurrentHit(HitCurrent);
// Return object HitCurrent
return HitCurrent;
})}
}
上面等待承诺的异步块是:
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
HitCurrent = await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
})();
我想获取更新的 HitCurrent
对象(如我所说修改为 exploreHitLine(HitCurrent, a, b, k, 'drawing');
),一旦 webworker 收到其结果(一个值和一个对象 HitResult)。
我不知道如何使 await computeHit(HitCurrent, 'computer');
的行为和我应用 Promise
结尾的 return 的行为,即:
return HitCurrent;
修正方案是什么? :
1) 正在做:
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
await computeHit(HitCurrent, 'computer');
与 Promise
:
return HitCurrent;
2) 做:
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
Object = await computeHit(HitCurrent, 'computer');
与 Promise
:
return HitCurrent;
对于 2) 的情况,如果这是解决方案,我怎样才能从局部变量 Object
取回 Hitcurrent
对象? 我看到结果可能是包装器。
我认为重要的一点是 return 承诺不同于 return 像 HitCurrent
这样的对象,不是吗?
目前,对于 webworker 的轻计算,此 post 的代码部分中给出的解决方案工作正常,但一旦我对 webworker 进行高计算,代码就会自行终止游戏,没有用户点击的更多交互(我用鼠标点击游戏板)。
所以,我想得到建议,以便在所有情况下从 Promise
块中取回对象 HitCurrent
,以实现 webworker 的轻量级和高计算量。
好的,看完演示后,我想我明白了它可能会崩溃的原因..
对于游戏模式(用户/计算机等),您有 currentGame
具有多种情况的函数,对于计算机模式,您正在调用 returns 承诺的函数。问题是 promise 被包装在立即调用的函数中,这意味着它下面的代码不会等待并继续执行。
为了说明我的意思,这里有一个例子:
var testPriomise = () => new Promise(resolve => { resolve(2)})
console.log('1');
(async() => {
var result = await testPriomise();
console.log(result)
})()
console.log('Look! I fire before 2!', 3)
如果您在控制台中 运行 它,您会注意到它记录了 1、3,然后才记录了 2。
我希望你明白我要说的地方:)
进行以下几项更改:
而不是
// Main game function : started with white player
function currentGame(HitCurrent) {
做
// Main game function : started with white player
async function currentGame(HitCurrent) {
对于您的计算机模式案例,请执行此操作:
// Play computer hit
else {
// First option : Wait computeHit function
await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
}
这样可以确保 resetSwitchUpdate
已更新 HitCurrent
对象。
我希望这会解决它!
我启动了一个 webworker(计算游戏中的命中)到一个 promise 中,我想在 webworker 完成计算后取回结果(总是一个数组)。
我尝试在 promise 结束时 return then(
之后的对象并在主线程中执行:
(async () => {
// Wait computeHit function
await computeHit(HitCurrent, 'computer');
但似乎当 computeHit
完成的计算很高时,promise
中的 return HitCurrent
和 (async () =>
块中的 HitCurrent = await
选项之间存在冲突.
看下面的代码就更清楚了:
背景包括:
1) webworker的使用
2) promise的使用
3) async/await 关键字
承诺块:
function computeHit(HitCurrent, mode) {
if (mode == 'computer') {
let HitTemp = JSON.parse(JSON.stringify(HitCurrent));
return new Promise( resolve => {
// Creation of webworker
firstWorker = new Worker(workerScript);
firstWorker.onmessage = function (event) {
resolve(event.data);
}
// Post current copy of HitCurrent, i.e HitCurrent
firstWorker.postMessage([HitTemp, HitTemp.playerCurrent, maxNodes]);
}).then(({result}) => {
// Get back game board of webworker
HitTemp = result.HitResult;
// Get back suggested hit computed by webworker
[a,b] = HitTemp.coordPlayable;
// Drawing all lines from suggested hit (in 8 directions)
// HERE, we modify HitCurrent attributes (like array)
for (k = 0; k < 8; k++) {
exploreHitLine(HitCurrent, a, b, k, 'drawing');
}
// Remove playable hits
cleanHits('playable', HitCurrent);
// Display current game
displayCurrentHit(HitCurrent);
// Return object HitCurrent
return HitCurrent;
})}
}
上面等待承诺的异步块是:
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
HitCurrent = await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
})();
我想获取更新的 HitCurrent
对象(如我所说修改为 exploreHitLine(HitCurrent, a, b, k, 'drawing');
),一旦 webworker 收到其结果(一个值和一个对象 HitResult)。
我不知道如何使 await computeHit(HitCurrent, 'computer');
的行为和我应用 Promise
结尾的 return 的行为,即:
return HitCurrent;
修正方案是什么? :
1) 正在做:
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
await computeHit(HitCurrent, 'computer');
与 Promise
:
return HitCurrent;
2) 做:
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
Object = await computeHit(HitCurrent, 'computer');
与 Promise
:
return HitCurrent;
对于 2) 的情况,如果这是解决方案,我怎样才能从局部变量 Object
取回 Hitcurrent
对象? 我看到结果可能是包装器。
我认为重要的一点是 return 承诺不同于 return 像 HitCurrent
这样的对象,不是吗?
目前,对于 webworker 的轻计算,此 post 的代码部分中给出的解决方案工作正常,但一旦我对 webworker 进行高计算,代码就会自行终止游戏,没有用户点击的更多交互(我用鼠标点击游戏板)。
所以,我想得到建议,以便在所有情况下从 Promise
块中取回对象 HitCurrent
,以实现 webworker 的轻量级和高计算量。
好的,看完演示后,我想我明白了它可能会崩溃的原因..
对于游戏模式(用户/计算机等),您有 currentGame
具有多种情况的函数,对于计算机模式,您正在调用 returns 承诺的函数。问题是 promise 被包装在立即调用的函数中,这意味着它下面的代码不会等待并继续执行。
为了说明我的意思,这里有一个例子:
var testPriomise = () => new Promise(resolve => { resolve(2)})
console.log('1');
(async() => {
var result = await testPriomise();
console.log(result)
})()
console.log('Look! I fire before 2!', 3)
如果您在控制台中 运行 它,您会注意到它记录了 1、3,然后才记录了 2。 我希望你明白我要说的地方:)
进行以下几项更改: 而不是
// Main game function : started with white player
function currentGame(HitCurrent) {
做
// Main game function : started with white player
async function currentGame(HitCurrent) {
对于您的计算机模式案例,请执行此操作:
// Play computer hit
else {
// First option : Wait computeHit function
await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
}
这样可以确保 resetSwitchUpdate
已更新 HitCurrent
对象。
我希望这会解决它!