How can I wait for the result of aras.evalMethod when the called Method opens a dialog?
Hi community,
I have a custom button inside Form that leads to a custom Method.
This Method calls a second client Method with aras.evalMethod.
The second Method does a few checks and then opens a "confirm" dialog. User have to press OK or cancel the dialog.
The overall concept works fine, but I cannot get a valid result of the aras.evalMethod call. The second Method shall return the result of the selected dialog option. But the first Method doesn´t wait for the result of the second Method. So my result will be empty / undefined.
First Method
[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:dfb2fd99-4ad3-47f9-a2ba-e5d476b7c094:type=javascript&text=const%20args%20%3D%20%22%22%3B%0Aconst%20result%20%3D%20aras.evalMethod%28%22MySecondMethod%22%2C%20document.thisItem%2C%20args%29%3B%0Aif%20%28typeof%20result%20%3D%3D%20%22object%22%20%7C%7C%20result%20%3D%3D%3D%20false%29%0A%7B%0A%20%20%20%20return%3B%0A%7D%0A%0Aalert%28result%29%3B%20%2F%2F%20We%20immeditelly%20get%20this%20alert%20after%20the%20Method%20call.%20It%20will%20be%20empty%2C%20as%20the%20dialog%20it%20self%20wasn%C2%B4t%20resolved%20yet.%0A%0A%2F%2F%C2%A0Other%20code%2C%20e.g.%20hide%20a%20button%0Aconst%20el%3Ddocument.getElementById%28%22myelement%22%29%3B%0Ael.style.visibility%20%3D%20%22hidden%22%3B]
Second Method
[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:70a41c6e-8770-4a71-bc45-938927eac638:type=javascript&text=%5B...%5D%0Awindow.ArasModules.Dialog.confirm%28message%2C%20dialogOptions%29.then%28%0Afunction%28res%29%20%0A%7B%0A%20%20%20%20switch%20%28res%29%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20case%20%27ok%27%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20dosomething%28%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%20%20%20%20default%3A%0A%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%7D%0A%7D%29%3B%0A]
I tried using promises and async/await. But so far aras.evalMethod never waited for anything.
I could merge the two Methods, but currently the second Method is called from multiple contexts. So this is something I want to avoid if possible.
Does anyone has an idea that could work to improve the situation?
Thanks!
Angela
Hi Angela,
I was able to get this working by returning the promise from the second method in this example, i.e.:
[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:3afa909f-12ea-4e15-8121-3ed0476bdb26:type=html&text=return%20window.ArasModules.Dialog.confirm%28message%2C%20dialogOptions%29.then%28%0Afunction%28res%29%20%0A%7B%0A%20%20%20%20switch%20%28res%29%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20case%20%27ok%27%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20dosomething%28%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%20%20%20%20default%3A%0A%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%7D%0A%7D%29%3B]
I could then handle the result of this promise in the first method by adding a `.then` statement to the aras.evalMethod call, i.e.:
[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:89a4d5e9-d7e9-45a5-acd6-bf288bdb7449:type=text&text=aras.evalMethod%28%22MySecondMethod%22%2C%20document.thisItem%2C%20args%29.then%28%28result%29%20%3D%3E%20%7B%0D%0A%20%20%20%20if%20%28typeof%20result%20%3D%3D%20%22object%22%20%7C%7C%20result%20%3D%3D%3D%20false%29%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20return%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20%0D%0A%20%20%20%20alert%28result%29%3B%20%2F%2F%20We%20immeditelly%20get%20this%20alert%20after%20the%20Method%20call.%20It%20will%20be%20empty%2C%20as%20the%20dialog%20it%20self%20wasn%C2%B4t%20resolved%20yet.%0D%0A%20%20%20%20%0D%0A%20%20%20%20%2F%2F%C2%A0Other%20code%2C%20e.g.%20hide%20a%20button%0D%0A%20%20%20%20const%20el%3Ddocument.getElementById%28%22myelement%22%29%3B%0D%0A%20%20%20%20el.style.visibility%20%3D%20%22hidden%22%3B%0D%0A%7D%29%0D%0A]
This isn't a perfect solution in that it does require changing the second method to return a promise which may interfere with how it's being used elsewhere. Let me know if this works for your use case though.