可以从承诺数组中删除承诺吗?

Adr*_*SIO 6 javascript node.js promise

我想创建类似处理同步行为和异步行为的东西。例如,我希望能够像这样:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我想删除从lunchedPromise返回finishedTest的承诺我已经尝试过的:

launchedPromise.splice( lunchedTests.indexOf(finishedTest), 1 );
launchedPromise = lunchedTests.filter(prom => prom !== finishedTest);
Run Code Online (Sandbox Code Playgroud)

它显然不起作用,因为 (finishedTest !== PromiseToLunch[0]) 它甚至不是相同的类型,但我需要测试 ^^。我也尝试访问 PromiseValue 没有成功。

如果我们只保留由 [*] 标记的 console.log()。我想得到以下输出:

{ "wait": 10, "nextIndex": 2, "id": 1 }  
{ "wait": 5, "nextIndex": -1, "id": 3 }]
{ "wait": 500, "nextIndex": -1, "id": 2 }
Run Code Online (Sandbox Code Playgroud)

Adr*_*SIO 3

所以,这篇文章的答案是这个函数:

for (let i = 0; i < LaunchedTests.length; i++) {
    if (LaunchedTests[i].id === finishedTest.scenario + finishedTest.name) {
        return Promise.resolve(LaunchedTests.splice(i, 1));
    }
}
return Promise.reject("not find");
Run Code Online (Sandbox Code Playgroud)

但首先你需要初始化一个 id,如下所示:

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;
Run Code Online (Sandbox Code Playgroud)

您的承诺将具有以下形式:Promise { <pending>, id: YourId }您将能够通过传递findIndex()函数来访问它,并且您只需使用splice它即可。

感谢@dx-over-dt 和大家对我的帮助!


归档时间:

查看次数:

689 次

最近记录:

6 年,1 月 前