Skip to Content
0
Dec 11, 2020 at 09:59 AM

How to write an Asynchronous action in CAP Node.js App?

67 Views Last edit Dec 11, 2020 at 10:00 AM 2 rev

Hi, experts,

We are using emit event to act as asynchronous request for data bulkcreate.

But it turns out synchronous.

Can you help me with it ?

the response 'accepted' was not returned until the emit process 'processBasicInfoEvent' was completely done.

Code snippets:

  srv.on('EmployeeBasicInfoImport', async (request) => {

    return 'accepted';
  });

  srv.after('EmployeeBasicInfoImport', async (data, request) => {
    var batchImportData = request.data;
      srv.emit('processBasicInfoEvent', batchImportData);
  });

  srv.on('processBasicInfoEvent', async (request) => {
    const sfsrv = await cds.connect.to('successfactors');
    var batchImportData = JSON.parse(JSON.stringify(request.data));
    var logID = batchImportData.logID;
    batchImportData = batchImportData.employees;
 
    for (var employee of batchImportData) {
    
      await process(employee, sfsrv);
    }
    processBasicInfologs(logID, batchImportData, 'processBasicInfoEvent');
    return batchImportData;
  });<br>