What interesting: if I define .then just as: .then(alert("in .then")); then it works fine. But if i use the function() inside or the notation you've provided: .then(output => { alert(output); });
then system never reaches the .then. What I'm doing wrong?
pt., 11 sty 2019 o 20:44 . magicznykrag@gmail.com napisał(a):
Martin,
Thank you very much for the answer! Unfortunately I'm still stuck :-( Please take a look at the following code and tell me why it's still asynchronous. It display properly messages: "testFunct" and then "end of testFunct", but it never reach the .then or .done. Thank you in advance!
function testFunc() { alert("testFunc"); lic = cockpit.spawn([
"/usr/share/cockpit/testSoftware/connectExternalSystem"], { directory: "/usr/share/cockpit/testSoftware", err: "message" }) .then(function() { alert("in .then of testFunc. Error: "+message); }) .done(function() { alert("in .done of testFunc. Error: "+message); }); alert("end of testFunc"); }
czw., 10 sty 2019 o 09:02 Martin Pitt martin@piware.de napisał(a):
Hello Magik,
. [2019-01-09 21:51 +0100]:
I'm C programmer learning now Cockpit. My problem is as follows: for
some
of the operation I must use external helper and I must wait for it
output.
The cockpit.spawn() function returns the promise, which is non-blocking operation (as expected). How can I make it work as blocking one? Which mean: the rest of the code in the function should be done after
receiving
full output of the cockpit.spawn (done() or fail())
This is a common pitfal when coming from the world of synchronous programming languages (such as C or Python) and starting with asynchronous JavaScript. Daniel's await proposal can certainly help to make async code look like it was written in a sync style; but honestly, it's really best to let go of this pattern when writing JS.
So instead of
output = cockpit.spawn(..).make_this_magically_sync(); followup_action(output);
actually use JavaScript promises as they are intended:
cockpit.spawn(..) .then(output => { followup_action(output); }) .catch(err => { console.error(err); });
i. e. organize the stuff that depends on the result into promise handlers.
Martin _______________________________________________ cockpit-devel mailing list -- cockpit-devel@lists.fedorahosted.org To unsubscribe send an email to cockpit-devel-leave@lists.fedorahosted.org Fedora Code of Conduct: https://getfedora.org/code-of-conduct.html List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines List Archives: https://lists.fedorahosted.org/archives/list/cockpit-devel@lists.fedorahoste...