SoFunction
Updated on 2025-03-03

The Express framework uses axios synchronization request (async+await) to implement the method

axios is generally used as an asynchronous request, but in some special cases, synchronous requests are required. How to implement it?

First define a method syncAxios

let axios = require('axios');

 = function (obj = {}) {
  let url =  "/api/";

  return new Promise((resolve, reject) => {
    axios(url, {
      method: 'POST',
      timeout: 5000,
      params: {
        sProcName: ,
        idNo: ,
        userName: ,
        overTime: 
      }
    }).then((res) => {
      resolve();
    }).catch((error) => {
      reject(error)
    })
  })
};

Then call in controllers

 = function (req, res) {
  //Define the async method body  (id).populate('author').exec(async function (err, result) { 
    let dataCode = false;
    if( ==0){
        //Synchronous call      await ({
          sProcName: 'Update',
          id: '000000-1111-2222-3333-9999999',
          qq: '391502069',//,
          endTime:  '2022/12/31 11:39:05'//
        }).then((data) => {
          (data, 'res');
        }).catch((err) => {
          (err && );
        });

    }
    (function (err, onewxtob) {
      if () {
        return ({
          status: !err
        })
      }
    });
  });
};

Use the template engine jade in the view, and you need to request itcheckAfter the delayed refresh page displays the request result

setTimeout(function () {
   $(location).attr('href',)
}, 1000)

SetTimeout here realizes the effect of delaying loading and refreshing the page, and combined with the interaction of the controller, the effect of synchronous operation is finally achieved.