javascript - 将修改后的对象从 promise 返回到 async() block

标签 javascript promise async-await web-worker

我启动了一个 webworker(计算游戏中的命中率)来 promise ,我想在 webworker 完成计算后取回结果(总是一个数组)。

我尝试在 promise 结束时返回 then( 之后的对象,并在主线程中执行:

(async () => {
    // Wait computeHit function
    await computeHit(HitCurrent, 'computer');

但似乎当 computeHit 完成的计算很高时,return HitCurrentpromiseHitCurrent = await 选项进入 (async () => block 。

下面的代码更清楚:

背景包括:

1) webworker的使用

2) promise的使用

3) async/await 关键字

promise block :

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;
})}
} 

上面等待 promise 的异步 block 是:

(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 结尾的返回,即:

返回命中电流;

什么是修正液? :

1)做:

(异步()=> { //等待 computeHit 函数并在 promise 完成时更新 HitCurrent await computeHit(HitCurrent, 'computer');

进入 Promise :

返回命中电流;

2)做:

(异步()=> { //等待 computeHit 函数并在 promise 完成时更新 HitCurrent Object = await computeHit(HitCurrent, 'computer');

进入 Promise :

返回命中电流;


对于 2) 的情况,如果这是解决方案,我怎样才能从局部变量 Object 取回 Hitcurrent 对象? 我看到结果可能是一个包装器。

我认为重要的一点是返回一个 promise 不同于返回一个像HitCurrent这样的对象,不是吗?

目前,对于 webworker 的轻计算,本文代码部分给出的解决方案工作正常,但一旦我对 webworker 进行了高计算,代码就会自行终止游戏,不再有用户点击的交互(我用鼠标在游戏板上点击)。

因此,我想获得建议,以便在所有情况下从 Promise block 中取回对象 HitCurrent,以实现 webworker 的轻型和高计算。

最佳答案

好的,看完演示后,我想我明白了它可能会崩溃的原因..

您有 currentGame 函数,针对游戏模式(用户/计算机等)有多种情况,对于计算机模式,您正在调用返回 promise 的函数。问题是 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 对象。

我希望这会解决它!

关于javascript - 将修改后的对象从 promise 返回到 async() block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53998094/

相关文章:

javascript - 使用 Base64 图像时 Canvas.ToDataUrl 存在安全违规

php - 将php插入js文件

javascript - 扩展 WinJS Promise 对象

c# - 任务等待调用误解

typescript - 如何在 Jest 中的 beforeAll/beforeEach 和测试之间共享数据?

asp.net - iframe 的 onload 事件未在 firefox 中触发(与 ASP.NET 文件上传相关)

javascript - 在悬停时显示另一个 div

javascript - typescript 2.1 with async/await 为 angularjs 生成 ES5/ES3 目标

javascript - 如何在 Node.js 函数中应用 Promise

c# - 使用 Task.WhenAll 但需要跟踪每个单独任务的成功