skip to Main Content

I’d like to return a Promise on a spécific function that is not async.

The result is undefined when I call the function.

My code :

async function _callAccessControl(response, rpaReservation) {

  if (rpaResource.access_control_identifier && !accessControlExist) {
    return soap.createClientAsync('https://ipevia.com/public/files/onvif/credential.wsdl', {
      endpoint: 'https://ipevia.com/index.php?OnvifServer',
      forceSoap12Headers: true
    }).then((credentialClient) => {
      return credentialClient.CredentialService.CredentialPort.CreateCredential({
        Credential: {
          CredentialHolderReference: holderReference,
          CredentialIdentifier: {
            Type: {
              Name: 'pt:PIN',
              FormatType: 'SIMPLE_NUMBER16'
            },
            Value: rpaReservation.access_code
          },
          CredentialAccessProfile: {
            AccessProfileToken: rpaResource.access_control_identifier,
            ValidFrom: `${rpaReservationDateFormatted} ${rpaScheduleGrid.start_hour}:00`,
            ValidTo: `${rpaReservationDateFormatted} ${rpaScheduleGrid.end_hour}:00`
          }
        }
      }, function(err, result) {
        return rpaReservation.update({
          access_control_identifier: parseInt(result.Token, 10)
        }).then((updatedReservation) => {
          return updatedReservation;
        });
      }, { auth });
    });
  } else {
    return rpaReservation;
  }
}

_callAccessControl(response, rpaReservation).then((result) => {
  // result is undefined
})

2

Answers


  1. Is this what you want?

    function myFunction() {
      return new Promise((resolve, reject) => {
        // Do something asynchronous here...
        setTimeout(() => {
          resolve('Success!');
        }, 1000);
      });
    }
    
    // Usage:
    myFunction().then(result => {
      console.log(result); // Output: Success!
    });
    
    

    I think resolve will resolve your problem. 🙂
    And reject will handle the error part.

    Login or Signup to reply.
  2. You need to wrap credentialClient.CredentialService.CredentialPort.CreateCredential in a promise as shown below:

    function createCredentialWithPromise(credentialClient) {
      return new Promise((resolve, reject) => {
        credentialClient.CredentialService.CredentialPort.CreateCredential(
          {
            ...
          },
          function (err, result) {
            if (err) reject();
            else resolve(result);
          },
          { auth }
        );
      });
    }
    

    and you can use it as shown below:

    function _callAccessControl(response, rpaReservation) {
      if (rpaResource.access_control_identifier && !accessControlExist) {
        return soap
          .createClientAsync(...)
          .then(credentialClient => {
            return createCredentialWithPromise(credentialClient);
          })
          .then(result => {
            return rpaReservation.update(...);
          })
      } else {
        return rpaReservation;
      }
    }
    

    or, you can use the async-await syntax:

    async function _callAccessControl(response, rpaReservation) {
      if (rpaResource.access_control_identifier && !accessControlExist) {
        const credentialClient = await soap.createClientAsync(...);
        const result = await createCredentialWithPromise(credentialClient);
        return rpaReservation.update(...);
      } else {
        return rpaReservation;
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search