我希望能够创建一个既能处理同步行为又能处理异步行为的类似于这样的东西。
例如,我希望能够做到下面这样:
function timeout(myJson) {
return new Promise(function (resolve, reject) {
setTimeout(resolve, myJson.wait, myJson);
});
}
async function funct() {
try {
let PromiseTolaunch = [{ "wait": 10, "nextIndex": 2, "id": 1 },
{ "wait": 500, "nextIndex": -1, "id": 2 },
{ "wait": 5, "nextIndex": -1, "id": 3 }];
let launchedPromise = [], finishedPromise;
launchedPromise.push(timeout(PromiseTolaunch[0]));
launchedPromise[0].id = PromiseTolaunch[0].id;
launchedPromise.push(timeout(PromiseTolaunch[1]));
launchedPromise[1].id = PromiseTolaunch[1].id;
while (launchedPromise.length !== 0) {
finishedPromise = await Promise.race(launchedPromise);
[*] console.log(finishedPromise); // Expected output: { "wait": 10, "nextIndex": 2 }
//console.log(launchedPromise); // Expected output : [Promise { { wait: 10, nextIndex: 2, id: 1 }, id: 1 }, Promise { <pending>, id: 2 } ]
//I want to :
//Remove the promise that just been executed from launchedPromise
//console.log(launchedPromise); // Expected output : [ Promise { <pending>, id: 2 } ]
if (finishedPromise.nextIndex !== -1) {
launchedPromise.push(timeout(PromiseTolaunch[finishedPromise.nextIndex]));
}
}
return Promise.resolve("done")
} catch (error) {
return Promise.reject(error);
}
}
我希望在不影响 launchedPromise 的情况下,移除返回 finishedTest 的 Promise。
我已经尝试过:
launchedPromise.splice( lunchedTests.indexOf(finishedTest), 1 );
launchedPromise = lunchedTests.filter(prom => prom !== finishedTest);
显然它不起作用,因为(finishedTest !== PromiseToLunch[0]),它们甚至不是相同的类型,但我需要测试 ^^。我还尝试过访问PromiseValue,但没有成功。
如果我们只保留由[*]标记的console.log(),我想得到以下输出:
{ "wait": 10, "nextIndex": 2, "id": 1 }
{ "wait": 5, "nextIndex": -1, "id": 3 }]
{ "wait": 500, "nextIndex": -1, "id": 2 }
lunch
时,是否应该是launch
? - Patrick RobertsPromiseToLaunch
中的每个对象添加一个id
属性,那么你可以使用launchedPromise.findIndex()
来查找.id === finishedPromise.id
的索引,然后将该索引剔除。 - dx_over_dtasync function
作为执行器传递给new Promise
! - BergiPromise.race
不允许我们找出哪个 promise 先被解决。你必须自己记录这个过程。请参见例如 https://dev59.com/0pnga4cB1Zd3GeqPaZGn#38778887、https://dev59.com/xFUL5IYBdhLWcg3wSWRu#50586391 或 https://dev59.com/aFkT5IYBdhLWcg3wAK9E#39197252 等方法。 - Bergi