Imagine having a class like this:
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/bindNodeCallback';
export class MyClass {
name: string;
doSomethingWithName(cb: (err) => void) {
const error = 'I have no name';
const success = 'My name is: ' + this.name;
if (!this.name) {
return cb(error)
}
return cb(success);
}
doSomethingWithNameAndParam(param: string, cb: (err) => void) {
const error = 'I have no name and param value is: ' + param;
const success = 'My name is: ' + this.name + ' and my param value is :' + param;
if (!this.name) {
return cb(error)
}
return cb(success);
}
}
MyClass
has methods that require a callback as the last parameter.
The goal is to utilize the bindNodeCallback
method of rxjs.Observable to generate an Observable for each method instead of using callbacks.
The first method works correctly without any issues. Below is the code snippet that creates an Observable:
export function myClassObjFunctionObs(myObj: MyClass): Observable<MyClass> {
return Observable.bindNodeCallback(myObj.doSomethingWithName).call(myObj);
}
Here's how you can use the above Observable:
import {MyClass} from './my-class';
import {myClassObjFunctionObs} from './my-class';
const myClass = new MyClass();
myClass.name = 'I am your class';
myClassObjFunctionObs(myClass)
.subscribe(
data => console.log('data', data),
err => console.error(err),
() => console.log('DONE')
)
The challenge lies in creating a similar solution for the method doSomethingWithNameAndParam
, which requires a parameter before the callback.
Please provide any assistance or guidance on achieving this task.