🖥️FE🖥️

Promise, Resolve, $watch

들눈 2023. 12. 2. 15:54

If you want to wait until `trResult` gets data before executing further code, you can use a promise or Vue's `$watch` functionality. Below are examples of both approaches.

### Using a Promise:

```javascript

//[GET] Res Params
this.requestTr("AGY0600", this.trBody).then(() => {
  console.log(this.trResult[0]);
  // Continue with the code that depends on this.trResult
});

// ...

methods: {
  requestTr(trCode, trBody) {
    let advancedBody = trBody;

    return new Promise(resolve => {
      this.$bizMOB.Network.requestTr({
        "_sTrcode": trCode,
        "_oHeader": {
          // ... (your header properties)
        },
        "_oBody": {
          ...Object.fromEntries(
            Object.entries(advancedBody).map(([key, value]) => [
              key.replace(/"/g, ''),
              value
            ])
          )
        },
        "_fCallback": (res) => {
          this.trResult = [JSON.parse(JSON.stringify(res.body.assetList))];
          resolve(); // Resolve the promise once trResult is set
        }
      });
    });
  },
},
```

### Using $watch:
```javascript

//[GET] Res Params
this.requestTr("AGY0600", this.trBody);

// Watch for changes in trResult
this.$watch('trResult', (newVal, oldVal) => {
  if (newVal && newVal.length > 0) {
    console.log(newVal[0]);
    // Continue with the code that depends on this.trResult
  }
});
```

Choose the approach that fits better with your code structure and requirements. Using promises can be especially useful when dealing with asynchronous operations.