getter & setter wrapper for settings

I'm trying to use a getter & setter pair as an interface for game.settings.get and game.settings.set. Is it possible to make the setter asynchronous so that I can use await when I made assignments to it. This is what my getter and setter currently look like:
class Ctg {
static get MODES() {
return game.settings.get(Ctg.ID, "modes");
}
static set MODES(value) {
return game.settings.set(Ctg.ID, "modes", value);
}
}
class Ctg {
static get MODES() {
return game.settings.get(Ctg.ID, "modes");
}
static set MODES(value) {
return game.settings.set(Ctg.ID, "modes", value);
}
}
I'd like to do something like this (pseudocode):
await ( Ctg.MODES = Ctg.MODES.push(["NPC", "isNPC"]) );
await ( Ctg.MODES = Ctg.MODES.push(["NPC", "isNPC"]) );
UU
Unknown User795d ago
DT
Daniel Thorp795d ago
game.settings.set returns a promise, but I think my code may be wrong because setters aren't supposed to return anything at all usually I just added the return because I was hoping to be able to await that promise later (as shows in my pseudo code). This works actually:
let obj = {
get value() {
return this._value;
},
set value(myPromise) {
return (async () => {
this._value = await myPromise;
})();
},
};
obj.value = new Promise(resolve => setTimeout(() => resolve("hi"), 1000));
let obj = {
get value() {
return this._value;
},
set value(myPromise) {
return (async () => {
this._value = await myPromise;
})();
},
};
obj.value = new Promise(resolve => setTimeout(() => resolve("hi"), 1000));
Not sure if awaiting the assignment is even possible, but that's what I want to do.
UU
Unknown User795d ago
M
Mana795d ago
I'd think setter's return value is ignored and any attempt get the value of it would invoke the getter. I'd honestly expect JS to runtime error from attempting to return in setter.
DT
Daniel Thorp795d ago
No, it doesn't.
M
Mana795d ago
That is bizarre.
DT
Daniel Thorp795d ago
Is it impossible to do what I'm trying to do then? (await the assignment)
DT
Daniel Thorp795d ago
There's an eslint rule that explains a bit: https://eslint.org/docs/rules/no-setter-return
ESLint - Pluggable JavaScript linter
no-setter-return - Rules
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
M
Mana795d ago
Returning the promise seems the only possible solution. But promises are dumb when worked with directly. Even that says the returned value is ignored, or is an error (in the JS runtime). With setter/getter stuff your only option pretty much logically is to store the intermediate promise and return that in the getter. It's kinda like how constructors can't be async.
DT
Daniel Thorp795d ago
Can you give an example of how I would do that?
M
Mana795d ago
Something like
class Ctg {
static _t_modes;
static get MODES() {
return this._t_modes ?? game.settings.get(Ctg.ID, "modes");
}
static set MODES(value) {
this._t_modes = game.settings.set(Ctg.ID, "modes", value).then(_ => this._t_modes = null);
}
}
class Ctg {
static _t_modes;
static get MODES() {
return this._t_modes ?? game.settings.get(Ctg.ID, "modes");
}
static set MODES(value) {
this._t_modes = game.settings.set(Ctg.ID, "modes", value).then(_ => this._t_modes = null);
}
}
It makes the getter kinda meh Why do you even need the modes getter to delay for the arbitrary chance the setting is changed right then?
UU
Unknown User795d ago
TM
TyphonJS (Michael)794d ago
One point of distinction is that accessors IE getters / setters were introduced in ES5 in 2009. This is a long time before the async proposal dropped and hit stage 1 in the TC39 process. async / await semantics were introduced in the ES7 timeframe more officially. As far as JS goes it wasn't until ES7 / ~2016/17 that things have been accelerating in a more rapid yearly pace for introduction for more features and there was a 6 year gap between ES5 and ES6. Whereas ES6 formalized basic class representation of the existing accessor functionality of ES5. Outside of whether it would make sense in the first place for the ability to have an async setter if you were designing things from scratch right now any new feature like this has to be backward compatible to some extent with what existed before.
DT
Daniel Thorp794d ago
Ok. I did what LukeAbby suggested and it seems to be working, so I'll archive this thread 🙂
Want results from more Discord servers?
Add your server
More Posts
scrollable tab contentsIt is possible, and you're firmly in the css and DOM structure side of development with this line ofJr devI'm cleaning out my bookmarks and accidentally came back to this article. Potentially useful startinHTCPackage: `hero-creation-tool`System Specific CSSI'm not 100% about this, but it is worth watching out that your solution doesn't actually end up beiadvancement-wipCommander register apitalking about permissions, im working on the registering api for the Commander, and I wanted to ask Azgaars Blurry MapHey all. I'm taking a look at my importer for Azgaar's Fantasy Map Generator and wondering how I canccjmk overlaybut for what I wanted I think I *needed* to append it there so that I can overlay the whole screencliits very very early alpha demoMonarch APIAlright, adventurous ones: I have a Beta version of Monarch with the new components API! Manifest: compacted chat cardsModules create problems to solve problems... Today's problem: "When I roll an attack, it eats the whdragdrop shenneneginsRight now, a user can drag a spell onto their character sheet. I store that in a 'spell' array and leffect mini modulesI've gotten to the point where I'm confident in releasing a slurry of tiny no-config Active Effect rdamage application hooksNew 5e Hook ideas, gimme yer thoughts: `Hooks.call('Actor5e.preDamageApplied')` args: `damageAmounthook conventionsRegarding hooks, is the convention that _all_ `pre...` hooks are executed on the machine that initiaexpand arraysIs there a way to `expandObject` except for an Array?edit-owned-item-effectI've made an abomination. This hacks its way past the limitations surrounding editing effects on ownItem Pile feedbackSimple is king!effects to chat@badideasbureau is the `temporary` flag you look for in Effects to Chat a convention from somewhere 5e AEsI've been messing pretty heavily with some Active Effect assumptions and mechanisms in 5e. This is m