0

I'm trying to do async/await inside async.parallel, but can't call callback. But if i remove async/await, the callback can be call. Below is the code

let parallelFunc = listCamId.map(camId => {
    return async function (callback) {
      let insertRuleTransParam = {
        rule_id: ruleId,
        camera_id: camId,
        vas_id: vasId,
        additional_parameter: additionalParameter
      }

      let configParamCamera = { where: { id: camId } };
      let configParamRule = { where: { id: ruleId } };

      try {
        let ruleTrans = await createRuleTransaction(insertRuleTransParam);
        let rule = await ruleService.find(configParamRule, "one");
        let cam = await cameraService.find(configParamCamera, "one");
        let uri = await constructCamUri(cam.protocol, cam.user_access, cam.password, cam.ip_address, cam.port, cam.path);
        let visionParam = await startVasRule(vasId, ruleTrans.id, camId, rule.rule_path, rule.rule_exec, uri);
        let updateRuleParam = {
          port_stream: visionParam.port,
          url_stream: visionParam.url,
          mmap_in: visionParam.mmap_in,
          mmap_out: visionParam.mmap_out
        }
        await updateRuleTransaction(updateRuleParam);
        callback(null);
      } catch (e) {
        callback(e);
      }
    }
  })

  return new Promise((resolve, reject) => {
    async.parallel(parallelFunc, function (err, results) {
      if (err) {
        reject(err);
      } else {
        resolve('success');
      }
    })
  });

can someone point out whats wrong with the code?

4
  • Never use the async.js library together with promises, even less with async/await syntax. Commented May 4, 2018 at 14:48
  • do you have suggestion for other way? inside try catch is a sync code, im trying to achieve parallel execution with those sync code for faster time Commented May 4, 2018 at 14:53
  • See my answer :-) Commented May 4, 2018 at 14:55
  • @BIllySutomo Bergi is right, async.js became moot with the introduction of promises to the specification. Commented May 4, 2018 at 15:01

2 Answers 2

3

Do not use the async.js library for promise code, and do not use callback parameters. Just use Promise.all to wait for multiple things:

let promises = listCamId.map(async function(camId) {
  let insertRuleTransParam = {
    rule_id: ruleId,
    camera_id: camId,
    vas_id: vasId,
    additional_parameter: additionalParameter
  }

  let configParamCamera = { where: { id: camId } };
  let configParamRule = { where: { id: ruleId } };

  let ruleTrans = await createRuleTransaction(insertRuleTransParam);
  let rule = await ruleService.find(configParamRule, "one");
  let cam = await cameraService.find(configParamCamera, "one");
  let uri = await constructCamUri(cam.protocol, cam.user_access, cam.password, cam.ip_address, cam.port, cam.path);
  let visionParam = await startVasRule(vasId, ruleTrans.id, camId, rule.rule_path, rule.rule_exec, uri);
  let updateRuleParam = {
    port_stream: visionParam.port,
    url_stream: visionParam.url,
    mmap_in: visionParam.mmap_in,
    mmap_out: visionParam.mmap_out
  }
  await updateRuleTransaction(updateRuleParam);
  return null;
});
return Promise.all(promises);
Sign up to request clarification or add additional context in comments.

Comments

0

You CAN use async/await inside async.js

There is a major mistake inside your code:

return async function (callback) {}

Async functions do not need or require a callback, they are shorthand for Promises

Here is a simple example of an async function that will work in async.js:

async function forAsync() {
  try {
    let user = await db.findOne({});
    return user // this is equivalent to resolve(user)
  } catch (e) {
    throw(e) // this is equivalent to reject(err)
  }
}

As a Promise this would be as follows:

function forAsync() {
  return new Promise((resolve,reject) => {
    db.findOne({}, (err,res) => {
      if (err) reject(err)
      let user = res;
      resolve(user);
    });
  }
}

As a callback this would be as follows:

function forAsync(callback) {
  db.findOne({}, (err,res) => {
    if (err) callback(err)     
    let user = res;     
    callback(null, user);
  });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.