Hi,
i had a quick question about promises.
i’m still quite new to the concept. I understand the principle (not that hard )
But one of the things i’m still struggling with is: How and when are they resolved?
in my case, i’m developing a new Kodi plugin, with a different library. That lib is a lot cleaner and works perfect with promises.
But my issue:
The library returns a connection through a promise, which is great!
But i need to access that connection on multiple places in the plugin. like sending commands to Kodi.
At this point i set the connection from the promise to a local variable that i use later in the plugin at a different command.
Which of course is ugly as hell
i do not want to initialize a new connection on each command i send.
current way:
KodiApi(@config.host,@config.port).then (connection) =>
@kodi = connection
And further on i use “@kodi” to send commands to the connection.
KodiApi(@config.host,@config.port) returns a promise. is it better to store this promise in a variable and use that one every time?
or does it then reconnects every single time?
like:
@kodi = KodiApi(@config.host,@config.port)
@kodi.then (connection) =>
#first commands here (listeners)
@kodi.then (connection) =>
#send commands
connection.send bla bla
In the example above, does it use the same connection for both of the code parts? or does it resolve the KodiApi twice, and also opens a connection twice?
if so, that would be the best way to do this?
Check if the variable has been resolved, and no errors before executing a command?