'Busboy doesn't work with async/await in azure function

module.exports = async function (context, req) {
  const name = context.bindingData.name;
  contractAccount = await near.account(contractName)
  switch (name) {
        case 'get_sale':
            get_sale_func(context, req);
            break;
        default:
            break;
}

function get_sale_func(context, req) {
    const busboy = new Busboy({ headers: req.headers });
    busboy.on('field', function(fieldname, val) {
    });
    busboy.on('finish', function() {
        log('Done parsing form!');
        context.res = {
            status: 200, 
            body: { result: "done" }
        };
    });
    busboy.write(req.body, function() {});
}

It does not return this response.

context.res = {
  status: 200, 
  body: { result: "done" }
};

enter image description here

But if I comment on this code contractAccount = await near.account(contractName) of line 3, it works. enter image description here

Is there any way to overcome this issue?



Solution 1:[1]

Await expressions make promise-returning functions behave as though they're synchronous by suspending execution until the returned promise is fulfilled or rejected.

In your case try to return Promise and try catch block to handle exception.

module.exports = async function (context, req) {
try{
        const name = context.bindingData.name;
        contractAccount = await near.account(contractName) 
        switch (name) {
        case 'get_sale':
            get_sale_func(context, req);
            break;
        default:
            break;
        }
}
catch(Exception ex)
{
    log.Info($"Exception found {ex.Message}");
}
}
function get_sale_func(context, req) {
    return new Promise((resolve, reject) => {
        const busboy = new Busboy({ headers: req.headers });
            busboy.on('field', function(fieldname, val) {
        });
        busboy.on('finish', function() {
            log('Done parsing form!');
            context.res = {
            status: 200,
            body: { result: "done" }
        };
        resolve(context.res)
    });
    busboy.write(req.body, function() {});
    resolve(req)
    });
}

Refer here

Solution 2:[2]

This worked. Just wrap the busboy section in Promise.

async function nft_tokens_for_owner_func(context, req) {
  return new Promise((resolve, reject) => {
    let owner_id;
    const busboy = new Busboy({
      headers: req.headers
    });
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
      file.on('data', function(data) {
        files.push({
          fieldname,
          mimetype,
          data
        });
      });
      file.on('end', function() {});
    });
    busboy.on('field', function(fieldname, val) {
      if (fieldname === 'owner_id') {
        owner_id = val;
      }
    });
    busboy.on('finish', async function() {
      log('Done parsing form!');

      try {
        const tokens = await account.viewFunction(contractName, 'nft_tokens_for_owner', {
          account_id: owner_id,
          from_index: '0',
          limit: 100
        });
        context.res = {
          status: 200,
          body: {
            result: tokens
          }
        };
        context.done();
      } catch (e) {
        context.res = {
          status: 500,
          body: {
            result: e.message
          }
        };
        context.done();
      }
      resolve(true);
    });
    busboy.write(req.body, function() {});
  });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DelliganeshS-MT
Solution 2 webstar