If we want to do file or network i/o when opening and closing a panel, can we return promises that are the required results of having an async open/close handler?
Will XD do the right thing with them? (Waiting for resolution.)
If we want to do file or network i/o when opening and closing a panel, can we return promises that are the required results of having an async open/close handler?
Will XD do the right thing with them? (Waiting for resolution.)
You can have an async function inside your show/hide function like this:
async function show(event) {
console.log("opened")
const result = await fetch("https://google.com");
console.log(result)
event.node.appendChild(create());
// this creates the panel UI
}
Note that, AFAIK, XD won’t wait for those operations to complete, so you;'ll likely want to have a loader/spinner of some sort so your panel doesn’t just render as a blank surface.
OK, thanks.
While I “have” you here, a puzzle. In the e2e-adobe-stock
sample, App.jsx
, there’s the code:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
prefs: props.prefs || null,
};
...
this.init();
}
async init() {
// load settings from persistent storage -- this is ASYNC
this.setState( { prefs: await PreferencesStore.createFromSettings() })
}
how does the App class creation get away with not being async, since it’s calling this.init()
, which is async?
So since all async
functions return Promise
s, I’m abusing that a little since I don’t need the constructor to wait until the settings are loaded (the state will just get updated), and I don’t care about the results of the promise. As such, I can call it like a normal function, trusting that it will eventually load in its settings.
IIRC, you can occasionally catch the panel in the act of doing this with a very brief “Loading Settings” text at the top. Usually the I/O is fast enough that you don’t see it, but it does happen occasionally.