On 06/18/2018 10:13 AM, Stef Walter wrote:
On 18.06.2018 16:09, Mark Reynolds wrote:
> Hello,
>
> So I need to loop over a bunch of cockpit.spawns, but after each one is
> done I need to print a success message
>
> for (var attr in mods){
> var cmd = [DSCONF, '-j', 'ldapi://%2fvar%2frun%2f' + server_id
+
> '.socket','config', 'replace'];
> cmd.push(attr + "=" + mods[attr]);
>
> cockpit.spawn(cmd, { superuser: true, "err":
> "message"}).done(function() {
> // Success update config_value dict
> config_values[attr] = mods[attr];
> console.log("Success for: " + attr); // But attr is now
> different!!!!!!
> }).fail(function(data) {
> // Log error and reset the html
> popup_err("Error", "Failed to update attribute: " + attr
+
> "\n\n" + data);
> $("#" + attr).val(config_values[attr]);
> });
> }
>
>
> The problem is that when the first promise finishes "attr" has already
> changed. Is there a way to pass/store a copy of "attr" to the spawn()
> command so that it can be accessed in done() & fail()? I'm not seeing
> anything in the docs to do this, but figured I'd ask.
In javascript this is typically solved using promises and recursion:
function step(mods) {
var attr = mods.pop();
if (!attr)
return; /* all done*/
cockpit.spawn(... something with attr ...)
.then(function(data) {
/* successfully ran command ... */
step(mods);
}, function(ex) {
/* failed to run command ... */
});
}
Or similar.
Thanks for the prompt response Stef - I'll give this a shot!
Stef
> Thanks,
>
> Mark
>
>
> _______________________________________________
> cockpit-devel mailing list -- cockpit-devel(a)lists.fedorahosted.org
> To unsubscribe send an email to cockpit-devel-leave(a)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.fedoraproject.org/archives/list/cockpit-devel@lists.fedorah...
>