I have the following code that does promise chaining but I want to chain the promise in methods for reusability. How can I do that? For instance below is an example
GetExistingCustomer(firstname, lastname, email)
.then((results) =>
{
if (result.status === "Successful")
{
existingContactId = results.items[0].contactId;
CreateNewContact(firstName, lastName, email).then((existingContactId) => {
EmailContactByID(emailCode, existingContactId).then(() => {
wixLocation.to("/Thankyoupage");
})
})
}
else
{
CreateNewContact(firstName, lastName, email).then((newcontactId) => {
EmailContactByID(emailCode, existingContactId).then(() => {
wixLocation.to("/Thankyoupage");
})
})
}
})
What I want to do is create a method called CreateNewContact but don’t know how to make the method so that it handles the promise.
GetExistingCustomer(firstname, lastname, email)
.then((results) => {
if (result.status === "Successful") {
existingContactId = results.items[0].contactId;
CreateNewContactAndEmail(firstName, lastName, email, emailCode, existingContactId);
}
else {
CreateNewContactAndEmail(firstName, lastName, email, emailCode, newcontactId);
})
export function CreateNewContactAndEmail(firstName, lastName, email, emailCode, contactId) {
CreateNewContact(firstName, lastName, email).then((contactId) => {
EmailContactByID(emailCode, contactId).then(() => {
wixLocation.to("/Thankyoupage");
})
}