Return a modfied object from a promise to async() block
I start a webworker (computing a hit into a game) into a promise and I would like to get back the results (always an array) once webworker has finished its computations.
I tried, at the end of promise, to return the object after the then(
and to do in main thread :
(async () => {
// Wait computeHit function
await computeHit(HitCurrent, 'computer');
But it seems when computation done by computeHit
is high, there are conflicts between return HitCurrent
from promise
and the HitCurrent = await
option into (async () =>
block.
It is clearer on the code below :
the background consists of :
1) the using of webworker
2) the using of promise
3) the async/await keywords
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;
})}
}
Async block waiting for promise above is :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
HitCurrent = await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
})();
I would like to get the updated HitCurrent
object (modified as I said into exploreHitLine(HitCurrent, a, b, k, 'drawing');
) and this, once webworker has received its results (a value and an object HitResult).
I don't know how to make the behavior of await computeHit(HitCurrent, 'computer');
and the return that I apply the end of Promise
, i.e :
return HitCurrent;
What is the correction solution ? :
1) doing :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
await computeHit(HitCurrent, 'computer');
with into Promise
:
return HitCurrent;
2) doing :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
Object = await computeHit(HitCurrent, 'computer');
with into Promise
:
return HitCurrent;
for 2) case, if this is the solution, how can I get back the Hitcurrent
object from local variable Object
? I saw that result may be a wrapper.
I think an important point is that returning a promise is different from returning an Object like HitCurrent
, isn't it ?
For the moment, for light computation into webworker, solution given in the code section of this post works fine but as soon as I have high computation for webworker, the code terminates itself the game, without no more interactions for the user hit (that I do with mouse click on game board).
So, I would like to get advices to get back the object HitCurrent
from Promise
block in all cases, for light and high computation of webworker.
javascript promise async-await web-worker
add a comment |
I start a webworker (computing a hit into a game) into a promise and I would like to get back the results (always an array) once webworker has finished its computations.
I tried, at the end of promise, to return the object after the then(
and to do in main thread :
(async () => {
// Wait computeHit function
await computeHit(HitCurrent, 'computer');
But it seems when computation done by computeHit
is high, there are conflicts between return HitCurrent
from promise
and the HitCurrent = await
option into (async () =>
block.
It is clearer on the code below :
the background consists of :
1) the using of webworker
2) the using of promise
3) the async/await keywords
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;
})}
}
Async block waiting for promise above is :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
HitCurrent = await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
})();
I would like to get the updated HitCurrent
object (modified as I said into exploreHitLine(HitCurrent, a, b, k, 'drawing');
) and this, once webworker has received its results (a value and an object HitResult).
I don't know how to make the behavior of await computeHit(HitCurrent, 'computer');
and the return that I apply the end of Promise
, i.e :
return HitCurrent;
What is the correction solution ? :
1) doing :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
await computeHit(HitCurrent, 'computer');
with into Promise
:
return HitCurrent;
2) doing :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
Object = await computeHit(HitCurrent, 'computer');
with into Promise
:
return HitCurrent;
for 2) case, if this is the solution, how can I get back the Hitcurrent
object from local variable Object
? I saw that result may be a wrapper.
I think an important point is that returning a promise is different from returning an Object like HitCurrent
, isn't it ?
For the moment, for light computation into webworker, solution given in the code section of this post works fine but as soon as I have high computation for webworker, the code terminates itself the game, without no more interactions for the user hit (that I do with mouse click on game board).
So, I would like to get advices to get back the object HitCurrent
from Promise
block in all cases, for light and high computation of webworker.
javascript promise async-await web-worker
Do you mind to post the whole function for this partAsync Function waiting for promise above :
?
– Uma
Jan 1 at 19:51
@Uma the async Function is actually the block(async () => { // Wait computeHit function and update HitCurrent when promise is done HitCurrent = await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
– youpilat13
Jan 1 at 20:01
@Uma . My main issue is that I don't know the concurrency or priority between the return of promise (return new Promise
) and thereturn HitCurrent
into then ofthen
of promise.
– youpilat13
Jan 1 at 20:07
1
What exactly do you mean by "there are conflicts"? How often are you callingcomputeHit
? If there are multiple conflicting calls, make sure not to use global variables.
– Bergi
Jan 1 at 20:46
@Bergi . You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 1 at 23:54
add a comment |
I start a webworker (computing a hit into a game) into a promise and I would like to get back the results (always an array) once webworker has finished its computations.
I tried, at the end of promise, to return the object after the then(
and to do in main thread :
(async () => {
// Wait computeHit function
await computeHit(HitCurrent, 'computer');
But it seems when computation done by computeHit
is high, there are conflicts between return HitCurrent
from promise
and the HitCurrent = await
option into (async () =>
block.
It is clearer on the code below :
the background consists of :
1) the using of webworker
2) the using of promise
3) the async/await keywords
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;
})}
}
Async block waiting for promise above is :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
HitCurrent = await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
})();
I would like to get the updated HitCurrent
object (modified as I said into exploreHitLine(HitCurrent, a, b, k, 'drawing');
) and this, once webworker has received its results (a value and an object HitResult).
I don't know how to make the behavior of await computeHit(HitCurrent, 'computer');
and the return that I apply the end of Promise
, i.e :
return HitCurrent;
What is the correction solution ? :
1) doing :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
await computeHit(HitCurrent, 'computer');
with into Promise
:
return HitCurrent;
2) doing :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
Object = await computeHit(HitCurrent, 'computer');
with into Promise
:
return HitCurrent;
for 2) case, if this is the solution, how can I get back the Hitcurrent
object from local variable Object
? I saw that result may be a wrapper.
I think an important point is that returning a promise is different from returning an Object like HitCurrent
, isn't it ?
For the moment, for light computation into webworker, solution given in the code section of this post works fine but as soon as I have high computation for webworker, the code terminates itself the game, without no more interactions for the user hit (that I do with mouse click on game board).
So, I would like to get advices to get back the object HitCurrent
from Promise
block in all cases, for light and high computation of webworker.
javascript promise async-await web-worker
I start a webworker (computing a hit into a game) into a promise and I would like to get back the results (always an array) once webworker has finished its computations.
I tried, at the end of promise, to return the object after the then(
and to do in main thread :
(async () => {
// Wait computeHit function
await computeHit(HitCurrent, 'computer');
But it seems when computation done by computeHit
is high, there are conflicts between return HitCurrent
from promise
and the HitCurrent = await
option into (async () =>
block.
It is clearer on the code below :
the background consists of :
1) the using of webworker
2) the using of promise
3) the async/await keywords
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;
})}
}
Async block waiting for promise above is :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
HitCurrent = await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
})();
I would like to get the updated HitCurrent
object (modified as I said into exploreHitLine(HitCurrent, a, b, k, 'drawing');
) and this, once webworker has received its results (a value and an object HitResult).
I don't know how to make the behavior of await computeHit(HitCurrent, 'computer');
and the return that I apply the end of Promise
, i.e :
return HitCurrent;
What is the correction solution ? :
1) doing :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
await computeHit(HitCurrent, 'computer');
with into Promise
:
return HitCurrent;
2) doing :
(async () => {
// Wait computeHit function and update HitCurrent when promise is done
Object = await computeHit(HitCurrent, 'computer');
with into Promise
:
return HitCurrent;
for 2) case, if this is the solution, how can I get back the Hitcurrent
object from local variable Object
? I saw that result may be a wrapper.
I think an important point is that returning a promise is different from returning an Object like HitCurrent
, isn't it ?
For the moment, for light computation into webworker, solution given in the code section of this post works fine but as soon as I have high computation for webworker, the code terminates itself the game, without no more interactions for the user hit (that I do with mouse click on game board).
So, I would like to get advices to get back the object HitCurrent
from Promise
block in all cases, for light and high computation of webworker.
javascript promise async-await web-worker
javascript promise async-await web-worker
edited Jan 4 at 2:25
youpilat13
asked Jan 1 at 18:56
youpilat13youpilat13
3921441
3921441
Do you mind to post the whole function for this partAsync Function waiting for promise above :
?
– Uma
Jan 1 at 19:51
@Uma the async Function is actually the block(async () => { // Wait computeHit function and update HitCurrent when promise is done HitCurrent = await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
– youpilat13
Jan 1 at 20:01
@Uma . My main issue is that I don't know the concurrency or priority between the return of promise (return new Promise
) and thereturn HitCurrent
into then ofthen
of promise.
– youpilat13
Jan 1 at 20:07
1
What exactly do you mean by "there are conflicts"? How often are you callingcomputeHit
? If there are multiple conflicting calls, make sure not to use global variables.
– Bergi
Jan 1 at 20:46
@Bergi . You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 1 at 23:54
add a comment |
Do you mind to post the whole function for this partAsync Function waiting for promise above :
?
– Uma
Jan 1 at 19:51
@Uma the async Function is actually the block(async () => { // Wait computeHit function and update HitCurrent when promise is done HitCurrent = await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
– youpilat13
Jan 1 at 20:01
@Uma . My main issue is that I don't know the concurrency or priority between the return of promise (return new Promise
) and thereturn HitCurrent
into then ofthen
of promise.
– youpilat13
Jan 1 at 20:07
1
What exactly do you mean by "there are conflicts"? How often are you callingcomputeHit
? If there are multiple conflicting calls, make sure not to use global variables.
– Bergi
Jan 1 at 20:46
@Bergi . You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 1 at 23:54
Do you mind to post the whole function for this part
Async Function waiting for promise above :
?– Uma
Jan 1 at 19:51
Do you mind to post the whole function for this part
Async Function waiting for promise above :
?– Uma
Jan 1 at 19:51
@Uma the async Function is actually the block
(async () => { // Wait computeHit function and update HitCurrent when promise is done HitCurrent = await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
– youpilat13
Jan 1 at 20:01
@Uma the async Function is actually the block
(async () => { // Wait computeHit function and update HitCurrent when promise is done HitCurrent = await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
– youpilat13
Jan 1 at 20:01
@Uma . My main issue is that I don't know the concurrency or priority between the return of promise (
return new Promise
) and the return HitCurrent
into then of then
of promise.– youpilat13
Jan 1 at 20:07
@Uma . My main issue is that I don't know the concurrency or priority between the return of promise (
return new Promise
) and the return HitCurrent
into then of then
of promise.– youpilat13
Jan 1 at 20:07
1
1
What exactly do you mean by "there are conflicts"? How often are you calling
computeHit
? If there are multiple conflicting calls, make sure not to use global variables.– Bergi
Jan 1 at 20:46
What exactly do you mean by "there are conflicts"? How often are you calling
computeHit
? If there are multiple conflicting calls, make sure not to use global variables.– Bergi
Jan 1 at 20:46
@Bergi . You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 1 at 23:54
@Bergi . You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 1 at 23:54
add a comment |
1 Answer
1
active
oldest
votes
ok, after looking at the demo I think I see a reason why it might be breaking..
You have currentGame
function with multiple cases for the game mode (user / computer, etc), and for computer mode you are calling that function that returns promise. The problem is that promise is wrapped in immediately invoked function which means that the code below it doesn't wait and keeps executing.
Just to illustrate what I mean, here's an example for you:
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)
If you run it in console you'll notice that it logs 1, 3 and only then 2.
I hope that you see where I'm going with it:)
Make these few changes:
Instead of
// Main game function : started with white player
function currentGame(HitCurrent) {
do
// Main game function : started with white player
async function currentGame(HitCurrent) {
And for your computer mode case do this :
// Play computer hit
else {
// First option : Wait computeHit function
await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
}
This way you ensure that the resetSwitchUpdate
has updated HitCurrent
object.
I hope this will fix it!
Sorry but in your suggested code, you have not includedpostMessage
intoonmessage
, have you ? Same thing forclose()
function.
– youpilat13
Jan 1 at 21:57
you are right, my bad!
– Uma
Jan 1 at 22:09
1
You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 2 at 0:37
Updated the answer, looking at demo really helps
– Uma
Jan 2 at 1:16
thanks a lot. Why my first solution was not working ? I thought that block(async () => { // Wait computeHit function and update HitCurrent when promise is done await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
would allow to wait for the execution ofcomputerHit
function. What you did is to remove the wrapper{async () => ... }()
and putasync
oncurrentGame
function ? what does it change towards my initial solution ? regards
– youpilat13
Jan 2 at 15:16
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998094%2freturn-a-modfied-object-from-a-promise-to-async-block%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
ok, after looking at the demo I think I see a reason why it might be breaking..
You have currentGame
function with multiple cases for the game mode (user / computer, etc), and for computer mode you are calling that function that returns promise. The problem is that promise is wrapped in immediately invoked function which means that the code below it doesn't wait and keeps executing.
Just to illustrate what I mean, here's an example for you:
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)
If you run it in console you'll notice that it logs 1, 3 and only then 2.
I hope that you see where I'm going with it:)
Make these few changes:
Instead of
// Main game function : started with white player
function currentGame(HitCurrent) {
do
// Main game function : started with white player
async function currentGame(HitCurrent) {
And for your computer mode case do this :
// Play computer hit
else {
// First option : Wait computeHit function
await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
}
This way you ensure that the resetSwitchUpdate
has updated HitCurrent
object.
I hope this will fix it!
Sorry but in your suggested code, you have not includedpostMessage
intoonmessage
, have you ? Same thing forclose()
function.
– youpilat13
Jan 1 at 21:57
you are right, my bad!
– Uma
Jan 1 at 22:09
1
You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 2 at 0:37
Updated the answer, looking at demo really helps
– Uma
Jan 2 at 1:16
thanks a lot. Why my first solution was not working ? I thought that block(async () => { // Wait computeHit function and update HitCurrent when promise is done await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
would allow to wait for the execution ofcomputerHit
function. What you did is to remove the wrapper{async () => ... }()
and putasync
oncurrentGame
function ? what does it change towards my initial solution ? regards
– youpilat13
Jan 2 at 15:16
|
show 2 more comments
ok, after looking at the demo I think I see a reason why it might be breaking..
You have currentGame
function with multiple cases for the game mode (user / computer, etc), and for computer mode you are calling that function that returns promise. The problem is that promise is wrapped in immediately invoked function which means that the code below it doesn't wait and keeps executing.
Just to illustrate what I mean, here's an example for you:
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)
If you run it in console you'll notice that it logs 1, 3 and only then 2.
I hope that you see where I'm going with it:)
Make these few changes:
Instead of
// Main game function : started with white player
function currentGame(HitCurrent) {
do
// Main game function : started with white player
async function currentGame(HitCurrent) {
And for your computer mode case do this :
// Play computer hit
else {
// First option : Wait computeHit function
await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
}
This way you ensure that the resetSwitchUpdate
has updated HitCurrent
object.
I hope this will fix it!
Sorry but in your suggested code, you have not includedpostMessage
intoonmessage
, have you ? Same thing forclose()
function.
– youpilat13
Jan 1 at 21:57
you are right, my bad!
– Uma
Jan 1 at 22:09
1
You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 2 at 0:37
Updated the answer, looking at demo really helps
– Uma
Jan 2 at 1:16
thanks a lot. Why my first solution was not working ? I thought that block(async () => { // Wait computeHit function and update HitCurrent when promise is done await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
would allow to wait for the execution ofcomputerHit
function. What you did is to remove the wrapper{async () => ... }()
and putasync
oncurrentGame
function ? what does it change towards my initial solution ? regards
– youpilat13
Jan 2 at 15:16
|
show 2 more comments
ok, after looking at the demo I think I see a reason why it might be breaking..
You have currentGame
function with multiple cases for the game mode (user / computer, etc), and for computer mode you are calling that function that returns promise. The problem is that promise is wrapped in immediately invoked function which means that the code below it doesn't wait and keeps executing.
Just to illustrate what I mean, here's an example for you:
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)
If you run it in console you'll notice that it logs 1, 3 and only then 2.
I hope that you see where I'm going with it:)
Make these few changes:
Instead of
// Main game function : started with white player
function currentGame(HitCurrent) {
do
// Main game function : started with white player
async function currentGame(HitCurrent) {
And for your computer mode case do this :
// Play computer hit
else {
// First option : Wait computeHit function
await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
}
This way you ensure that the resetSwitchUpdate
has updated HitCurrent
object.
I hope this will fix it!
ok, after looking at the demo I think I see a reason why it might be breaking..
You have currentGame
function with multiple cases for the game mode (user / computer, etc), and for computer mode you are calling that function that returns promise. The problem is that promise is wrapped in immediately invoked function which means that the code below it doesn't wait and keeps executing.
Just to illustrate what I mean, here's an example for you:
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)
If you run it in console you'll notice that it logs 1, 3 and only then 2.
I hope that you see where I'm going with it:)
Make these few changes:
Instead of
// Main game function : started with white player
function currentGame(HitCurrent) {
do
// Main game function : started with white player
async function currentGame(HitCurrent) {
And for your computer mode case do this :
// Play computer hit
else {
// First option : Wait computeHit function
await computeHit(HitCurrent, 'computer');
// Reset, switch and update
resetSwitchUpdate(HitCurrent, false);
}
This way you ensure that the resetSwitchUpdate
has updated HitCurrent
object.
I hope this will fix it!
edited Jan 2 at 2:54
answered Jan 1 at 20:47


UmaUma
37615
37615
Sorry but in your suggested code, you have not includedpostMessage
intoonmessage
, have you ? Same thing forclose()
function.
– youpilat13
Jan 1 at 21:57
you are right, my bad!
– Uma
Jan 1 at 22:09
1
You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 2 at 0:37
Updated the answer, looking at demo really helps
– Uma
Jan 2 at 1:16
thanks a lot. Why my first solution was not working ? I thought that block(async () => { // Wait computeHit function and update HitCurrent when promise is done await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
would allow to wait for the execution ofcomputerHit
function. What you did is to remove the wrapper{async () => ... }()
and putasync
oncurrentGame
function ? what does it change towards my initial solution ? regards
– youpilat13
Jan 2 at 15:16
|
show 2 more comments
Sorry but in your suggested code, you have not includedpostMessage
intoonmessage
, have you ? Same thing forclose()
function.
– youpilat13
Jan 1 at 21:57
you are right, my bad!
– Uma
Jan 1 at 22:09
1
You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 2 at 0:37
Updated the answer, looking at demo really helps
– Uma
Jan 2 at 1:16
thanks a lot. Why my first solution was not working ? I thought that block(async () => { // Wait computeHit function and update HitCurrent when promise is done await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
would allow to wait for the execution ofcomputerHit
function. What you did is to remove the wrapper{async () => ... }()
and putasync
oncurrentGame
function ? what does it change towards my initial solution ? regards
– youpilat13
Jan 2 at 15:16
Sorry but in your suggested code, you have not included
postMessage
into onmessage
, have you ? Same thing for close()
function.– youpilat13
Jan 1 at 21:57
Sorry but in your suggested code, you have not included
postMessage
into onmessage
, have you ? Same thing for close()
function.– youpilat13
Jan 1 at 21:57
you are right, my bad!
– Uma
Jan 1 at 22:09
you are right, my bad!
– Uma
Jan 1 at 22:09
1
1
You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 2 at 0:37
You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 2 at 0:37
Updated the answer, looking at demo really helps
– Uma
Jan 2 at 1:16
Updated the answer, looking at demo really helps
– Uma
Jan 2 at 1:16
thanks a lot. Why my first solution was not working ? I thought that block
(async () => { // Wait computeHit function and update HitCurrent when promise is done await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
would allow to wait for the execution of computerHit
function. What you did is to remove the wrapper {async () => ... }()
and put async
on currentGame
function ? what does it change towards my initial solution ? regards– youpilat13
Jan 2 at 15:16
thanks a lot. Why my first solution was not working ? I thought that block
(async () => { // Wait computeHit function and update HitCurrent when promise is done await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
would allow to wait for the execution of computerHit
function. What you did is to remove the wrapper {async () => ... }()
and put async
on currentGame
function ? what does it change towards my initial solution ? regards– youpilat13
Jan 2 at 15:16
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998094%2freturn-a-modfied-object-from-a-promise-to-async-block%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you mind to post the whole function for this part
Async Function waiting for promise above :
?– Uma
Jan 1 at 19:51
@Uma the async Function is actually the block
(async () => { // Wait computeHit function and update HitCurrent when promise is done HitCurrent = await computeHit(HitCurrent, 'computer'); // Reset, switch and update resetSwitchUpdate(HitCurrent, false); })();
– youpilat13
Jan 1 at 20:01
@Uma . My main issue is that I don't know the concurrency or priority between the return of promise (
return new Promise
) and thereturn HitCurrent
into then ofthen
of promise.– youpilat13
Jan 1 at 20:07
1
What exactly do you mean by "there are conflicts"? How often are you calling
computeHit
? If there are multiple conflicting calls, make sure not to use global variables.– Bergi
Jan 1 at 20:46
@Bergi . You can check the game on the link I provided into my UPDATE 1 : you will see that everything is working fine for a depth between 1 and 3 but for depth = 4, I am losing the hit of user (with mouse click), i.e the game terminates itself (black cases).
– youpilat13
Jan 1 at 23:54