gpt4 book ai didi

javascript - js 生成器的异步问题并 promise 不返回结果

转载 作者:行者123 更新时间:2023-12-03 02:00:38 26 4
gpt4 key购买 nike

我遇到了另一个异步问题,我迷失了方向,不知道在哪里或如何解决它。请原谅我不好的命名。

api 调用 twitch api 并返回其结果的数组。

  exports.batchPromiseWrapper = function(arr) {
const filteredMungedDataArr = [];

let promiseBatachArray = arr.map(vod_id => {
var url = `https://api.twitch.tv/kraken/videos/${vod_id.id}/markers`;
var params = { api_version: 5 };

return axios
.get(url, {
params: params,
headers: {
"Client-ID": "xxxxxxxxxxxxxxx"
}
})
.then(res => {
return res.data;
})
.catch(function(error) {
console.log(error);
});
});

return Promise.all(promiseBatachArray)
.then(markers => {
if (markers !== null) {
markers.map(markerObj => {
if (markerObj.markers.game_changes !== null) {
markerObj.markers.game_changes.forEach(gameName => {
if (gameName.label === "Fortnite") {
filteredMungedDataArr.push(markerObj);
}
});
}
});
return filteredMungedDataArr;
}
})
.catch(err => {
if (err.status === 500 || err.status === 404) {
console.log("error: ", err, err.message);
}
});
};

数据如下:[[1,2,3,4,5],[1,2,3,4,5]],生成器将产生并做出 promise 。在暂停 5 秒并继续下一批 5 之前调用 5。

exports.batchFetchingGeneratorWrapper = function(generator, batchArray) {
let evalNextValue = generator.next();

let delay = (v, t) => {
return new Promise(resolve => {
setTimeout(resolve.bind(null, v), t);
});
};

if (!evalNextValue.done) {
exports.batchPromiseWrapper(evalNextValue.value).then(data => {
let newBatchArray = batchArray;
if (data !== undefined) {
newBatchArray = batchArray.concat(data);
}

delay(5000).then(() => {
exports.batchFetchingGeneratorWrapper(generator, newBatchArray);
});
});
} else {
console.log("yay done!", batchArray);
return batchArray;
}
};

我能够从batchFetchingGeneratorWrapper控制台batchArray中的结果,但我无法对其采取行动,并且我知道它与异步有关以及它尚未解决。

promiseDataWrapper
.then(data => {
return gatherData.cleanUpVODData(data);
})
.then(data => {
function* batchFetching(batchArray) {
for (let i = 0; i < batchArray.length; i++) {
yield batchArray[i];
}
}

let batchArrResult = [];
let g = batchFetching(data);

new Promise((resolve, reject) => {
gatherData.batchFetchingGeneratorWrapper(g, batchArrResult);

if (g.done) { // i dont think this works
console.log("batchArrResult 1: ", batchArrResult);
resolve(batchArrResult);
}
}).then(result => console.log("asdfasdf", batchArrResult)); // empty array is returned
});

最佳答案

据我所知,问题主要在于batchFetchingGeneratorWrapper()

这应该是一个问题:

  • 修复delay()
  • 做出适当的返回以使递归起作用
  • 确保函数返回 Promise

毫无疑问(语法上)使用 async/await 更简单,但这里是老式的 then :

exports.batchFetchingGeneratorWrapper = function(generator, batchArray) {
let evalNextValue = generator.next();
let delay = (t) => {
return new Promise(resolve => {
setTimeout(resolve, t);
});
};
if (!evalNextValue.done) {
return exports.batchPromiseWrapper(evalNextValue.value).then(data => {
return delay(5000).then(() => {
return exports.batchFetchingGeneratorWrapper(generator, batchArray.concat(data || []));
});
});
} else {
console.log("yay done!", batchArray);
return Promise.resolve(batchArray); // <<< promise wrapped to ensure that batchFetchingGeneratorWrapper() returns Promise
}
};

并适本地链接 batchFetchingGeneratorWrapper() 调用:

promiseDataWrapper
.then(data => gatherData.cleanUpVODData(data))
.then(data => {
function* batchFetching(batchArray) {
for (let i = 0; i < batchArray.length; i++) {
yield batchArray[i];
}
}
return gatherData.batchFetchingGeneratorWrapper(batchFetching(data), []).then(batchArrResult => {
console.log('batchArrResult: ', batchArrResult);
return batchArrResult;
});
}).catch(error => {
console.log(error);
});

关于javascript - js 生成器的异步问题并 promise 不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50053291/

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