A
A
Andrew2021-03-21 15:48:29
Yandex maps
Andrew, 2021-03-21 15:48:29

How to handle an asynchronous loop?

Good afternoon. Task: calculate the distance from one point to two others, then dispatch to the state. Problem: distances are calculated, but because array processing occurs asynchronously, the last state is dispatched.

const createRoute = () => {
        currentCrews.map((crew) => {
            let multiRoute = new props.maps.multiRouter.MultiRoute({referencePoints:[props.currentUserCoords,[crew.lat,crew.lon]], params: {routingMode:'pedestrian'}})
            multiRoute.model.events.add('requestsuccess', () => {
                let activeRoute = multiRoute.getActiveRoute().getPaths()
                activeRoute.each((path, i) => {                    
                    crew.distanceText = path.properties.get("distance").text
                    crew.distanceValue = path.properties.get("distance").value
                })
            })
            return crew
        })
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question