Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I receive the async data as the resolved value?

I’m trying to read files and convert them to a json, then upload them to a server. But I can’t seem to be able to get the data back that isn’t a promise. Can you point to what I’m doing wrong?

const onSubmit = async (formData: FormValues) => {
    remove();
    append(defaultFormValues.documents[0] as object);
    setIsLoading(true);
    const objToUpload = {
        name: formData.documentName,
        type: formData.documentType,
        contents: [
            formData.documents.map(async (document) => {
                return {
                    language: document.language,
                    data: await readFromFile(document.file[0]),
                    actions: await readFromFile(document.actions[0]),
                };
            }),
        ],
    };
    console.log(objToUpload);
    }
};

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const onSubmit = async (formData: FormValues) => {
    remove();
    append(defaultFormValues.documents[0] as object);
    setIsLoading(true);

    const data  = await Promise.all(formData.documents.map(async (document) => {
      return {
          language: document.language,
          data: await readFromFile(document.file[0]),
          actions: await readFromFile(document.actions[0]),
      };
    }));

    const objToUpload = {
        name: formData.documentName,
        type: formData.documentType,
        contents: data,
    };
    console.log(objToUpload);
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading