gpt4 book ai didi

javascript - postman /pm api sendRequest 命令 : how to wait till the response is back before populating the variable?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:31 25 4
gpt4 key购买 nike

我正在尝试使用 postman 的 pm api sendRequest 创建响应 json 的字典对象。

写了一个递归函数来获取所有响应,但问题是响应字典对象填充发生在响应返回之前。

有没有办法在收到每个相应的响应之前等待字典填充,以便在字典对象中捕获响应?

var respDictionary  = {};

getResponses (listOfUrls);

console.log("respDictionary: ");
console.log(respDictionary);

function getResponses(urlList) {

if (typeof urlList === 'string') {
urlList = urlList.split(' ');
}
_url = urlList[0];

var call = {
url: _url ,
method: 'GET',
header: {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json"
}
};
urlList.splice(0, 1);

pm.sendRequest(
call,
function (err, res) {
if (err) {
console.log(err);
} else {
if (urlList.length === 0) {
return;
}
try {
respDictionary[_url] = res.json();
} catch (e) {
console.log(err);
}
getResponses(urlList);
}
});
console.log(respDictionary);
}

输出是:

respDictionary:
Object:{}
//further, pm request responses are listed

最佳答案

你不了解 JavaScript 异步处理。也许以下内容会有所帮助:

如果您使用 promises,您的代码将有效:

function getResponses(urlList) {

if (typeof urlList === 'string') {
urlList = urlList.split(' ');
}
return Promise.all(
urlList.map(
function(url){
return {
url: url ,
method: 'GET',
header: {
//not sure where token comes from
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json"
}
};
}
).map(
function(call){
return new Promise(
function(resolve,reject){
pm.sendRequest(
call,
function (err, res) {
if (err) {
reject(err);
} else {
resolve([call.url,res.json()]);
}
});
}
)
.then(
undefined
,function(err){
//if something goes wrong we will still return something
return [call.url,{error:err}];
}
)
}
)
)
.then(
function(results){
return results.reduce(
function(acc,result){
acc[result[0]] = result[1];
}
,{}
);
}
)
}
getResponses (listOfUrls)
.then(//this will always succeed, failed items have {error:something}
function(results){
console.log("results:",results);
}
);
console.log("this comes before results");

上面的代码会导致所有请求同时发生,这可能不是想要的行为,我写了一个 throttle method这是一些可能派上用场的库函数的一部分。您可以对您的代码应用 throttle ,以便它只有最大数量的连接:

const max = 10;//maximum 10 active connections
function getResponses(urlList) {
const throttled = throttle(max);
if (typeof urlList === 'string') {
urlList = urlList.split(' ');
}
return Promise.all(
urlList.map(
function(url){
return {
url: url ,
method: 'GET',
header: {
//not sure where token comes from
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json"
}
};
}
).map(
throttled(//only max amount of connections active at any time
function(call){
return new Promise(
function(resolve,reject){
pm.sendRequest(
call,
function (err, res) {
if (err) {
reject(err);
} else {
resolve([call.url,res.json()]);
}
});
}
)
.then(
undefined
,function(err){
//if something goes wrong we will still return something
return [call.url,{error:err}];
}
)
}
)
)
)
.then(
function(results){
return results.reduce(
function(acc,result){
acc[result[0]] = result[1];
}
,{}
);
}
)
}
getResponses (listOfUrls)
.then(//this will always succeed, failed items have {error:something}
function(results){
console.log("results:",results);
}
);
console.log("this comes before results");

关于javascript - postman /pm api sendRequest 命令 : how to wait till the response is back before populating the variable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47405625/

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