Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait the end of a function before to start the next

I'm working on Ionic v4 with Angular.

In my project i use the BLE to communicate with a raspberry.

I have several step :

  1. Search Device around me
  2. Connect to this device
  3. Activate Notification
  4. Send Messages

Currently i have something like :

this.ble.scan().subscribe(result => {
  if (device === theDeviceIWant) {
    this.ble.connect(device.id).subscribe(result => {
      this.ble.startNotification(infosaboutDevice).subscribe(result => {
        // Message 1
        this.ble.writeWithoutResponse(infos, message).then(result => {
          // Message 2
          this.ble.writeWithoutResponse(infos, message).then(result => { 
            // Message 3
            this.ble.writeWithoutResponse(infos, message).then(result => {
              // Message X
              this.ble.writeWithoutResponse(infos, message).then(result => {
              })
            })
          })
        })
      })
    })
  })
}

I want to do something like that :

this.myScan();
this.myConnect();
this.myNotification();
this.myMessage('Text 1');
this.myMessage('Text 2');
this.myMessage('Text X');

The probleme : My function ‘myConnect‘ don't wait the end of ‘myScan‘ to start. So somme stuff needed by ‘myConnect‘ is do in ‘myScan‘.

I already try to use ‘async/await‘ but does not work. I think i don't use it correctly :

 await this.myConnect().then(async () => {
       await this.myNotification().then(async () => {
           await this.myMessage('03020000').then(async () => {
               await this.myMessage('010100').then(async () => {
                   await this.myMessage('020200' + this.random.toString(16));
               });
           });
       });
   });

Help me to understand how to create a function who wait the end of the before one to start :D

like image 510
Amaury Laroze Avatar asked Mar 03 '23 03:03

Amaury Laroze


2 Answers

Just use async/await OR then

 await this.myConnect();  // this awaits the Promise returned by myConnect to be resolved
 await this.myNotification();  // same for this Promise
 await this.myMessage('03020000');  // and so on...
 await this.myMessage('010100');
 await this.myMessage('020200' + this.random.toString(16));
like image 54
hansmaad Avatar answered Mar 07 '23 16:03

hansmaad


The keyword await makes JavaScript wait until that promise settles and returns its result.

So you dont need to use then in await this.myConnect().then(()=>{});

use await this.myConnect();

Below is example which help you understand better

function SignalOne() {

        return new Promise((resolve, reject) => {
            setTimeout(()=>{
                resolve('Hello iam signal one');
            }, 2000);
        });

    }

    function SignalTwo() {

        return new Promise((resolve, reject) => {
            setTimeout(()=>{
                resolve('Hello iam signal Two');
            }, 1000);
        });

    }

    async function sendSignal() {
        let one = await SignalOne();
        let two = await SignalTwo();
        console.log(one);
        console.log(two);

    }

    sendSignal();
like image 33
Saurabh Yadav Avatar answered Mar 07 '23 17:03

Saurabh Yadav