0

The following is my code

let icsFileData = [];
icsFileData = filterAttachmentArray.map(async(file) => {
  let buff = new Buffer(file.data, 'base64');
  let text = buff.toString('ascii');
  const data = await ical.async.parseICS(text);
  const objectKeys = Object.values(data);
  const filterObj =
    objectKeys.length === 1 || objectKeys[0].type === 'VEVENT' ? objectKeys[0] : objectKeys[1];
  const desiredObj = {
    subject: filterObj.summary.val ? filterObj.summary.val : filterObj.summary,
    description: filterObj.description.val ? filterObj.description.val : filterObj.description,
    dateStart: moment(filterObj.start).format('YYYY-MM-DDTHH:mm:ss\\Z'),
    dateEnd: moment(filterObj.end).format('YYYY-MM-DDTHH:mm:ss\\Z'),
    organizer: filterObj.organizer.params.EMAIL ?
      filterObj.organizer.params.EMAIL :
      filterObj.organizer.val.split('mailto:').join(''),
    invites: filterObj.attendee.length === undefined ?
      filterObj.attendee.params.EMAIL ?
      filterObj.attendee.params.EMAIL :
      filterObj.attendee.val.split('mailto:').join('') :
      filterObj.attendee.map(
        (invite) =>
        invite.params.EMAIL ? invite.params.EMAIL : invite.val.split('mailto:').join('')
      ),
    location: filterObj.location.val ? filterObj.location.val : filterObj.location
  };
  icsFileData.push(desiredObj);
});
//const icsFileDataArray = await Promise.all(icsFileData);
console.log('jhgjhgjhgj: ', await icsFileData);

Please check what is the issue, I tried many times always get the same result.

Here is the result this code always returns me

[ Promise { <pending> } ]
5
  • can you add a snippet of your code, that's better than a picture, also would help addressing your issue. Commented Mar 13, 2020 at 5:13
  • i have add please check Commented Mar 13, 2020 at 5:15
  • Promise{} means that you have to use .then() to check the value. Use something like this const returnFromAwait = await icsFileData(); then try returnFromAwait.then(res => console.log(res)); Commented Mar 13, 2020 at 5:23
  • not working i tried this one Commented Mar 13, 2020 at 5:34
  • Promise {<resolved>: undefined} proto: Promise [[PromiseStatus]]: "resolved" Getting something like this? [[PromiseValue]]: undefined Commented Mar 13, 2020 at 5:40

1 Answer 1

2

Things to understand,

  1. async/await are just a syntactical sugar to write async code in synchronous fashion
  2. the callback inside your .map(async() => { ...}) gonna return a Promise.
  3. so in your code, icsFileData is a collection of Promise, so you have to use Promise.all to resolve all the promises to get the desired result
let icsFileData = [];
icsFileData = filterAttachmentArray.map(async(file) => {
  ....
  // you dont need this line, you can simply return the desiredObj
  // icsFileData.push(desiredObj);
  return desiredObj;
});
// this does the magic, since, icsFileData is a array of Promise
const icsFileDataArray = await Promise.all(icsFileData);
console.log('result: ', icsFileDataArray);
Sign up to request clarification or add additional context in comments.

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.