0

I have written a promise.all function as follow which is not returning my expected result.

if (Array.isArray(data)) {
    return Promise.all(data.map((data) => {
        hook.app.service('location')
        .find({ query: { serviceLocationId:"100" } })
        .then((result) => {
            var startDept = result.data[0];
            if (result.data.length > 0) {
                data.problem.solutions.solution.routes.route.forEach((d) => {
                    d.stops.unshift({
                        serviceType: '',
                        orders: [],
                        travelTime:'',
                        travelDistance:'',
                        travelCost:'',
                        serviceTime: '',
                        serviceTimeCost:0,
                        tripNumber:0,
                        stopName : startDept.description,
                        arrivalTime:'',
                        departureTime : 36000.0,
                        locationType : startDept.locationType,
                        sequence : 0,
                        locationId : startDept.serviceLocationId,
                        lng : startDept.location.lng,
                        lat : startDept.location.lat
                    });
                    var endDept = d.stops.pop();
                    d.stops.push({
                        serviceType: '',
                        orders: [],
                        travelTime:endDept.travelTime,
                        travelDistance:endDept.travelDistance,
                        travelCost:endDept.travelCost,
                        serviceTime: '',
                        serviceTimeCost:0,
                        tripNumber:0,
                        stopName : startDept.description,
                        arrivalTime : endDept.arrivalTime,
                        departureTime:'',
                        locationType : startDept.locationType,
                        sequence : endDept.sequence,
                        locationId : endDept.locationId,
                        lng : startDept.location.lng,
                        lat : startDept.location.lat
                    });
                });
                hook.data = data;
                combineArr = [];
                return routes(hook).then((hook) => {
                    if ([hook.data].length > 0) {
                        combineArr.push(hook.data);
                        return combineArr;
                    } else {
                        next(new Error('routes response create failed'));
                    }
                }).catch((err) => { return next(new Error(err.error)); });
            } else {
                next(new Error('no depo found'));
            }
        }).catch(err=>{ return next(new Error(err.error)); });
    })).then((results) => {
        console.log(results);
        hook.result = results;
        next();
    });
}

The promise above returns [null, null]. I'm not getting the expected result. Please help me to resolve this.

M0nst3R
  • 5,186
  • 1
  • 23
  • 36
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
  • Please have a read through [mcve]. If you do what it describes, it helps you diagnose and correct problems like this (or if you still can't figure it out, it gives you a nice, contained example to use when asking others for help). – T.J. Crowder Oct 24 '17 at 09:24

1 Answers1

8

You're not returning anything from the map callback, so the resulting array is an array of undefined, not promises. You need to return the promise you're getting in the callback so the array is an array of promises.

Basically, you're doing this:

return Promise.all(myArray.map(entry => {
    getAPromise(entry);
}));

...where you should be doing

return Promise.all(myArray.map(entry => {
    return getAPromise(entry);
}));

....or

return Promise.all(myArray.map(entry => getAPromise(entry)));
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875