Ad

Cant Return The Chained Promises Result

- 1 answer

const request = require('request-promise')

required this module and use it in this way the data and subData is options that i defined later...

    const foo= (data, subData) => {
    return request(data)
     .then(result => {
        console.log('result:',result)
        return request(subData)
    })
}

the problem is the request(data) result is not return but the request(subData) result is return

Q.allSettled([
  foo(),
  fo(),
  f(),
  .
  .
  .
])

and with q module create an array of promises, but i still cant get my expected return result

Ad

Answer

You can use any of the following methods to chain your promises and return both responses into an array

const foo = (data, subData) => {
    let result;
    return request(data)
        .then(res => {
            result = res;
            return request(subData)
        }).then(res => {
            return [result, res]
        });
}

//OR
const foo2 = (data, subData) => {
    return request(data)
        .then(res1 => {
            return request(subData).then(res2 => {
                return [res1, res2]
            })
        });
}

//OR
const foo3 = async (data, subData) => {
    let res1  = await request(data);
    let res2 = await request(subData);
    return [res1, re2];
}
Ad
source: stackoverflow.com
Ad