JavaScript heartbeat function encapsulation - polling interface to query data status

Function Design Requirements

This function solves the following requirements

  • The function is a packaged asynchronous function
  • Promise is always in padding state when polling has not reached the termination state
  • When polling has not reached the termination state, the interface needs to be requested every five seconds
  • The polling interface is waitingCheckImportV2
  • The status value success is success
  • The status value failed is success

1 Introduction

This heartbeat function is an asynchronous function, and its function is to continuously access the incoming id until the access is successful. On each access, the function waits five seconds before making the next access. If the access is successful, return a Promise in resolved state, whose value is id; otherwise, return a Promise in rejected state.

In practical applications, this function is usually used to monitor the completion of an operation, such as uploading files to the server or importing data to the database.

2. Function implementation

The function body code is as follows:

async function continuousAccess(id) {
    
    
  await new Promise((resolve) => setTimeout(() => resolve(), 5000))
  const {
    
     data, msg } = await waitingCheckImportV2({
    
     threadName: id })
  if (data === 'success') {
    
    
    return Promise.resolve(id)
  }
  if (data === 'failed') {
    
    
    ElMessage.error(msg || '计算失败')
    return Promise.reject()
  }
  return continuousAccess(id)
}

Among them, await new Promise((resolve) => setTimeout(() => resolve(), 5000))it means to wait for five seconds before performing the next operation.

Then, by calling another asynchronous function waitingCheckImportV2, get the state information (including data and information) of the specified id. If the data is 'success', that is, the access is successful, return a Promise in a resolved state, whose value is id; if the data is 'failed', that is, the access fails, call the ElMessage.error method to output an error message, and return a rejected state Promise; otherwise continue to the next visit.

If there is still no successful access for multiple consecutive accesses, the function will call itself recursively until the access is successful.

3. Precautions

  • The parameter id must be passed in to the function, otherwise the function cannot be executed;
  • Before using this function, you need to define the asynchronous function waitingCheckImportV2 (poll purpose function), and ensure that it can obtain the status information of the specified id;
  • After calling the function, the returned Promise should be processed so that the corresponding operation can be performed when the access succeeds or fails.

4. Practical application

In practical applications, the following code can be used to call the heartbeat function:

continuousAccess(someId)
  .then(id => {
    
    
    console.log(`访问 ${
      
      id} 成功!`)
    // 执行成功后的操作
  })
  .catch(() => {
    
    
    console.log('访问失败!')
    // 执行失败后的操作
  })

Among them, someId is the target id that needs to be accessed. When the access is successful, it will output "Access someId succeeded!"; when the access fails, it will output "Access failed!". You can add operations that need to be performed at the corresponding positions according to actual needs.

5. Summary

This heartbeat function is a simple and practical asynchronous function, mainly used to monitor the completion of an operation. Its realization method is relatively simple, and its applicability is strong, and it can be applied in many occasions. When using this function, you need to pay attention to the handling of parameter passing and Promise in order to execute the code correctly.

Guess you like

Origin blog.csdn.net/weixin_44599143/article/details/130504807