我想创建类似处理同步行为和异步行为的东西。例如,我希望能够像这样:
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 …Run Code Online (Sandbox Code Playgroud)