gpt4 book ai didi

node.js - 无法返回链式 promise 结果

转载 作者:太空宇宙 更新时间:2023-11-03 23:53:36 25 4
gpt4 key购买 nike

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

最佳答案

您可以使用以下任何方法来链接您的 promise 并将两个响应返回到数组中

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];
}

关于node.js - 无法返回链式 promise 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58372400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com