DEV Community

Raj
Raj

Posted on

dealing with async method calls

newNote is optional and if available should be pushed to notes colleciton via reducer using addProjectNotes function hook. The createProject methods seems called before the addProjectNotes completes execution and Notes collection is always blank. How can I call createProject only after addProjectNotes is fully executed?

'''
if (projectData.newNote) {
addProjectNotes(addNote(projectData.newNote))
}
createProject(projectData, setIsSubmitting, submittedCallback);
'''

Top comments (1)

Collapse
 
vasylnahuliak profile image
Vasyl Nahuliak

Hello. You can use async/await.

const nameYourFunction = async () => {
if (projectData.newNote) {
await addProjectNotes(addNote(projectData.newNote))
}
createProject(projectData, setIsSubmitting, submittedCallback);
}