Chain class methods with callbacks?

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.

+4
source share
3 answers

You use the return this chain for synchronous calls.

You use a Promise chain or callbacks for asynchronous calls.

 class MyClass { get() { return new Promise((resolve, reject)=> { // Make Request and call resolve( result ) or reject( err ) }); } set( property, value ) { this[property] = value; return this; } save() { return new Promise((resolve, reject)=> { // Make Request and call resolve( result ) or reject( err ) }); } } var myObject = new MyClass(); myObject .set('something', 'taco') .save() // resolve handler .then(( saveResult )=> { console.log( saveResult ); return myObject.get(); }) // resolve handler .then(( getResult )=> { console.log( getResult ); }) // reject handler .catch(( error )=> { }); 
+4
source

You can save the special __lastPromise member __lastPromise . It will initially be enabled by default. But then any function that performs the task will update it with a promise of return. And also the function itself will perform its task only after the previous promise is saved.

Like this:

 save() { // only after __lastPromise has resolved var newPromise = __lastPromise.then(function() { // Do stuff here }); // update __lastPromise with the newly returned promise this.__lastPromise = newPromise; // return this for regular chaining return this; } 

Full class:

 class MyClass { constructor(){ this.__lastPromise = Promise.resolve(); this.a = 0; } set(property, value) { var self = this; self.__lastPromise = self.__lastPromise.then(function() { return new Promise(function(resolve, reject) { console.log('self.%s set to: ', property, value); self[property] = value; resolve(); }); }); return self; } save() { var self = this; self.__lastPromise = self.__lastPromise.then(function() { return new Promise(function(resolve, reject) { console.log('Saved'); resolve(); }); }); return self; } } var myObject = new MyClass(); myObject.set('a', '1').save().set('a', '2').save(); 
 this.a set to: 1 Saved this.a set to: 2 Saved 
0
source

Your method might be something like this:

 (async function() { class MyClass { constructor() { this._lastPromise = Promise.resolve(); this.a = 0; } set(property, value) { var self = this; return new Promise(async (resolve, reject) => { console.log("self.%s set to: %s", property, value); await new Promise((resolve, reject) => { setTimeout(resolve, 5000); }); self[property] = value; resolve(self); }); } save() { var self = this; return new Promise(function(resolve, reject) { console.log(self.a + " Saved"); resolve(self); }); } } var myObject = new MyClass(); console.log("before"); await myObject .set("a", "1") .then(self => self.save()) .then(self => self.set("a", "5")) .then(self => self.save()); console.log("after"); })(); 
0
source

Source: https://habr.com/ru/post/1234059/


All Articles