Reputation: 4612
I have a couple of events that I listen to. A function in the second event html.set
depends on the function in the first event initialised
completing first. I've come up with a solution using promises, but it feels dirty. I'm wondering if this is the best way?
var applyGoogleFonts = new Promise(function(resolve, reject) {
editor.events.on('initialized', function() {
_applyGoogleFonts().then(resolve);
});
});
editor.events.on('html.set', function() {
applyGoogleFonts.then(_setThemeFonts);
});
I should add that _setThemeFonts
depends on the code before the trigger of html.set
completing first.
Upvotes: 0
Views: 56
Reputation: 12197
Not sure you'll find this cleaner, but just to suggest an alternative:
const waitForInit = new Promise(resolve => editor.events.on('initialized', resolve));
const waitForHtml = new Promise(resolve => editor.events.on('html.set', resolve));
waitForInit.then(_applyGoogleFonts).then(() => waitForHtml).then(_setThemeFonts);
It's important to subscribe to both events before waiting for the first one, because otherwise the second event could fire before the second subscription was added. Your code does this correctly as well.
Upvotes: 2