google cloud api | test list url map | add a google bucket web map | Search

The waitForOperation function waits for a specified Google operation to complete and returns the target link when finished, using a recursive approach with a 500ms delay between checks.

Run example

npm run import -- "wait for cloud operation"

wait for cloud operation

var importer = require('../Core');
var authorizeGoogle = importer.import("authorize google service");

function waitForOperation(operation) {
    return authorizeGoogle()
        .then(client => client.request({
            url: `${operation}`,
        }))
        .then(res => {
            if(res.data.status != 'RUNNING') {
                return Promise.resolve(res.data.targetLink);
            }
            console.log(`waiting for ${operation}`);
            return new Promise(resolve => setTimeout(resolve, 500))
                .then(() => waitForOperation(operation));
        })
  
}

module.exports = waitForOperation;

What the code could have been:

```javascript
/**
 * Imports the required modules.
 * @module Core
 * @module authorize-google-service
 */
const { authorizeGoogle } = require('../Core');

/**
 * Waits for a Cloud Operations API operation to complete.
 * @async
 * @param {string} operation - The operation to wait for.
 * @returns {string} - The target link of the operation.
 */
async function waitForOperation(operation) {
    try {
        // Import the authorizeGoogle function.
        const client = await authorizeGoogle();

        // Make a GET request to the Cloud Operations API.
        const response = await client.request({
            url: `${operation}`,
        });

        // Check if the operation is not running.
        if (response.data.status!== 'RUNNING') {
            // Return the target link of the operation.
            return response.data.targetLink;
        }

        // Log a message to the console indicating that we need to wait for the operation.
        console.log(`Waiting for ${operation}...`);

        // Implement a retry mechanism with a delay.
        await retryOperation(operation, 500);

        // If the operation is still not complete after the retries, throw an error.
        if (response.data.status!== 'DONE') {
            throw new Error(`Operation ${operation} did not complete after retries.`);
        }

        // Return the target link of the operation.
        return response.data.targetLink;
    } catch (error) {
        // Log the error to the console and rethrow it.
        console.error(`Error waiting for operation ${operation}:`, error);
        throw error;
    }
}

// Define a helper function to implement the retry mechanism.
async function retryOperation(operation, delay) {
    await new Promise(resolve => setTimeout(resolve, delay));
    return waitForOperation(operation);
}

// Export the waitForOperation function.
module.exports = waitForOperation;
```

Function Breakdown

waitForOperation(operation)

Purpose: Waits for an operation to complete and returns the target link when finished.

Parameters: operation (string) - URL of the operation.

Dependencies:

Flow:

  1. Authorizes Google services using authorizeGoogle.
  2. Makes a request to the specified operation URL.
  3. Checks if the operation is complete (status is not 'RUNNING').
  4. Returns the target link when the operation is complete.

Exported Function

The waitForOperation function is exported as a module, making it available for use in other parts of the application.