this is my first try with promises. i am not sure i understood them correctly, but anyway i have 3 functions that return a promise of status code. below is one such function-
export function createWaypoints(organisationId: string, waypoints: Waypoint[]): Promise<{ status: number }> {
return new Promise((resolve) => {
for (let index = 0; index < waypoints.length; index += 1) {
apiClient
.graphQlRequest<{ createWaypoint: Waypoint }, MutationCreateWaypointArgs>({
query: `mutation createWaypoint($input: CreateWaypointInput!) {
createWaypoint(input: $input) {
id
name
address
location
}
}`,
variables: {
input: {
organisationId,
name: waypoints[index].name,
address: waypoints[index].address,
location: [waypoints[index].latitude, waypoints[index].longitude],
},
},
})
.then((createWaypoint) => {
resolve({ status: createWaypoint.status });
});
}
});
}
i want chain these functions based on the status response and only execuite if the status is 200. i am calling them like so-
createWaypoints(dataContainer.organisationId, data.waypoints).then(({ status }) => {
if (status === 200) {
createEmployeeProfiles(dataContainer.organisationId, data.employeeProfiles, 'Drivers').then(({ status }) => {
if (status === 200) {
createPassengerProfiles(dataContainer.organisationId, data.employeeProfiles);
}
});
}
});
However, i get this error-
The following error originated from your test code, not from Cypress. It was caused by an unhandled promise rejection.
> Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.
The command that returned the promise was:
> cy.request()
The cy command you invoked inside the promise was:
> cy.getLocalStorage()
Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.
Cypress will resolve your command with whatever the final Cypress command yields.
The reason this is an error instead of a warning is because Cypress internally queues commands serially whereas Promises execute as soon as they are invoked. Attempting to reconcile this would prevent Cypress from ever resolving.
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Because this error occurred during a before all hook we are skipping all of the remaining tests
not sure how to resolve it, any help highly appreciated. thanks a lot!