0

Hi I have this function that I have written.

GetIndex method

  getIndex(req: connreq){
  var found = false;
  var number =0;
  this.firereq.child(req.recipient).once('value', function(snapshot) {
    if (!snapshot.exists()) {
      console.log('Not exists');
      this.x = '0';
    }
  });

  }

And I call it, in this function.

SendCommand method

 async sendcommand(req: connreq, cmd: string) {
this.getIndex(req);
  var promise = new Promise((resolve, reject) => {
    this.firereq
    .child(req.recipient)
    .child(this.x) .set({
      sender: req.sender,
      Command: cmd,
    })
    .then(() => {
      resolve({ success: true });
    })
    .catch(err => {
      resolve(err);
    });
    });
      return promise;


}

However it seems that the second function continues before the getIndex method has completed. I assume we can use awaits and promises to force the sendcommand function to wait for the Getindex to finish before proceeding to the rest of it's tasks. Any assistance would be appreciated

1
  • The same way sendcommand is promisified, you can also promisify getIndex. Commented Sep 23, 2019 at 12:13

3 Answers 3

1

Return a promise in the getIndex() method and execute the second in a then call back. So generally, the getIndex should be:

getIndex(req: connreq){
    return new Promise((resolve,reject) =>
    {
        var found = false;
        var number =0;
        this.firereq.child(req.recipient).once('value', 
        function(snapshot) {
           if (!snapshot.exists()) {
               console.log('Not exists');
               this.x = '0';
               reject('Not exists');
            }
            resolve('exists');
        });
     });
}

The above will trigger the catch block if not found and the then block if found so generally it should be:

this.getIndex(req)
.then(() => {
    this.firereq
    .child(req.recipient)
    .child(this.x) .set({
          sender: req.sender,
          Command: cmd,
    })
    .then(() => {
       resolve({ success: true });
     })
    .catch(err => {
       resolve(err);
    })
})
.catch(err => {
  resolve(err);
});
Sign up to request clarification or add additional context in comments.

Comments

1

In the first function do the following:

 getIndex(req: connreq){
  return new Promise((resolve, reject) =>
  let found = false;
  let number =0;
  this.firereq.child(req.recipient).once('value', function(snapshot) {
    if (!snapshot.exists()) {
       reject("not exists");
      console.log('Not exists');
      this.x = '0';
       }
    else{
       resolve(snapshot.val());
         }
      });
    });
  }

Then you can do:

 async sendcommand(req: connreq, cmd: string) {
this.getIndex(req).then((data)=>
  {
  var promise = new Promise((resolve, reject) => {
    this.firereq
    .child(req.recipient)
    .child(this.x) .set({
      sender: req.sender,
      Command: cmd,
    })
    .then(() => {
      resolve({ success: true });
    })
    .catch(err => {
      resolve(err);
    });
    });
    });
      return promise;


}

This way getIndex() will return a promise and everything insde then() will be executed after getIndex() returns.

Comments

1

Return Promise from getIndex and await it in sendCommand function

    getIndex(req: connreq){
      return new Promise((resolve, reject) => {
      var found = false;
      var number =0;
      this.firereq.child(req.recipient).once('value', function(snapshot) {
        if (!snapshot.exists()) {
          console.log('Not exists');
          this.x = '0';
        }
      });

      resolve();
      }
    });

and

    async sendcommand(req: connreq, cmd: string) {
        await this.getIndex();
        ....
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.