In Node.js, how do you combine class methods together when is it best to use callbacks?
In my PHP days, I usually did something like this:
class MyClass { function get() { // fetch some data from an API endpoint return this; } function set(property, value) { // Set the property to some value on the object return this; } function save() { // Save the data to database return this; } } $myObject = new MyClass(); $myObject->set('something','taco')->save();
This very common OO approach has allowed you to combine methods together as many times as you like.
When working with Node.js, can you somehow relate this to this? Or do you just get into the addon? Is each "chain" a nested callback?
Or do I just need to wrap my script in Promises?
new Promise(function(resolve, reject){ var myObject = new MyClass(); myObject.set('something','taco'); resolve(myObject); }).then(function(myObject){ myObject.save(); });
Is that how you should do it? Is there a way to integrate this more deeply into my class, so I don't need to embed it in promises every time? I saw that some libraries have a kind of "promising mode", for example https://github.com/sindresorhus/got , but after looking at the code, Iβm still not quite how they did it.
source share