我的函数原型如下
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
class GversionsBulkUpdate extends React.Component {
......
update = async () => {
const { handleLoadingClose, handleLoading, appStore } = this.props;
handleLoading();
let bulkMax = 10; // 最大并发值
const fails = [];
await new Promise((resolve) => {
const isFinish = finish(this.state.list.length, resolve); // finish mark
this.state.list.forEach((pkginfo) => {
while (bulkMax <= 0) await sleep(300); // 限制并发数量
bulkMax -= 1;
const body = { address: pkginfo.url, gversion: pkginfo.gversion, ftype: pkginfo.ftype };
goGameRequest.createPfile(appStore.user, pkginfo.pkg.package_id, body,
() => { console.log('finish one'); bulkMax += 1; isFinish.next(); }, // error callback
(msg) => { // success callback
console.log(msg);
bulkMax += 1;
isFinish.next();
fails.push({ pkg: pkginfo.pkg, msg });
});
});
});
console.log(fails)
const msg = fails.length === 0 ? '批量更新执行完毕' : '部分包更新失败';
handleLoadingClose(msg);
this.setState({ parameters: Object.assign({}, BaseParameters),
list: Object.assign([], baseList),
packages: Object.assign([], this.pacakges) });
};
......
createPfile 是个 http 请求
想用 while (bulkMax <= 0) await sleep(300); 限制并发请求数量
但是语法错误....Promise 中需要等待要如何写..有点懵比了
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.