Flu
Flu

Reputation: 49

Return out of a void function

I have a read() function with an arrow-function as a parameter. If I implement the function like this, the value is shown in the console as it should.

function(path: string) {
    this.db.read(path, val => {
      console.log(val.value)
    });
}

My question is, how can I get the value out of this function?

I tried it in several return ways, but nothing was worked.

//This is not working...
function(path: string) {
  newValue = this.db.read(path, val => {
      console.log(val.value)
      return val.value;
    });
  return newValue;
}

//Is working, but not really good...
private newValue;

function(path: string) {
  this.db.read(path, val => {
      console.log(val.value)
      this.newValue = val.value;
    });
  return this.newValue;
}

Maybe someone can give me a hint on how to do that.

Upvotes: 2

Views: 850

Answers (3)

Sunil
Sunil

Reputation: 11243

Your case is related Asynchronous calls. So its very important that you play with Observable, Callback and Subscription.

Since read returns the Observable already, you don't need to create new one.

What needs to be changed ?

So that needs to be changed in your code. All is fine except the way you are calling it. Do not return the value instead let it return the Observable.

Solution 1

readData(path){
   new Promise((resolve, reject)=>{
      this.db.read(path, val => {
          let readValue = val.value;  //<-- you can choose what you want to return.
          resolve(readValue);
       });
   });

//usage

readData("/path/of/db").then(value=>{
   console.log(value); //value is here once the DB call is completed.
});

Solution 2

The same thing can be achieved by Observable if you want to notify you every time new data is received.

private _subject: Subject = new Subject();

readData(path){
      this.db.read(path, val => {
          let readValue = val.value;  //<-- you can choose what you want to return.
          subject.next(readValue);
       });
}

usage

this.subject.subscribe((data) => {
    console.log('Data from DB ', data);
  });

Upvotes: 0

Saad Mehmood
Saad Mehmood

Reputation: 731

Try this approach. Return new Promise instead of val that you will get in some future time.:

function readDb(path: string) {
  return new Promise(function(resolve, reject) {
    this.db.read(path, val => {
      resolve(val.value);
    });
  });
}
this.readDb("/path").then(function(val) { console.log(val) });

Upvotes: 4

Matthias Cruciani
Matthias Cruciani

Reputation: 9

the 2nd solution you gave is the way to achieve that.

When you do this.newValue = val.value; you affect the value you get from your read function to the variable newValue, which is exactly what you want, therefore the value is already outside of the function.

private newValue;

function(path: string) {
  this.db.read(path, val => {
    this.newValue = val.value;
  });
}

You don't need to return anything here since you already have the value stored on your private variable newValue.

Upvotes: -1

Related Questions