Search code examples
javascriptnode.jspromisefeathersjs

Promise.all return [null, null] and not return as expected


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.


Solution

  • 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)));