Home > OS >  How can I receive the async data as the resolved value?
How can I receive the async data as the resolved value?

Time:06-02

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);
    }
};

CodePudding user response:

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);
};
  • Related