最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

解析Javascript承诺列表

运维笔记admin17浏览0评论

解析Javascript承诺列表

解析Javascript承诺列表

我的实际代码非常复杂,但是我会尽可能简化:

let counter = 0
console.log("time counter: ", counter)

setInterval(() => {
    counter = counter + 1;
    console.log("time counter: ", counter)
}, 1000)

const myPromises =
  [ new Promise((resolve, reject) => setTimeout(() => {
                console.log("reject(1)")
                reject(1)
        }, 5 * 1000)) // after 5 seconds.

    , new Promise(resolve => setTimeout(() => {
                console.log("resolve(2)")
                resolve(2)
        }, 3 * 1000)) // after 3 seconds.

    , new Promise(resolve => setTimeout(() => {
                console.log("resolve(3)")
                resolve(3)
        }, 3 * 1000))   // after 3 seconds.

    , new Promise((resolve, reject) => setTimeout(() => {
                console.log("reject(4)")
                reject(4)
        }, 1 * 1000))   // after 1 second.

  ]

async function testIt(){
    const results = myPromises.map(async promise => {
            return new Promise((resolve) => {
                // no matter what happens with the individual promise itself, we resolve.
                promise
                    .then(ok => {
                        resolve({ wasSuccessful: true, result: ok })
                    })
                    .catch(err => {
                        resolve({ wasSuccessful: false, error: err })
                    })
            })
    })

    // should be no need to catch anything here. use await.
    const results_ = await Promise.all(results)

    console.log("results_: ", results_)
}

testIt()
    .catch(err => console.log("this error isn't supposed to happen error: ", err))

我本质上想要以下内容:

1. start the first promise( myPromises[0] ). Wait 5 seconds. After that reject it.

2. start the next promise( myPromises[1] ). Wait 3 seconds. Resolve it.

此刻我们还有8秒钟的时间。

3. start the next promise( myPromises[2] ). Wait another 3 seconds. Resolve it.

此时,计数器上有8 + 3 = 11秒。

4. start the next promise ( myPromises[3] ).. Wait for 1 second.. resolve it.

我想您是个主意。现在该怎么做?

请注意,这不是then().then().then()。.不减少或累积此列表,正如我在该主题的其他问题中所见。我不希望由于任何原因而拒绝这样做。

相反,我想要一个结果列表。像这样:

results_:  [
  { wasSuccessful: false, error: 1 },
  { wasSuccessful: true, result: 2 },
  { wasSuccessful: true, result: 3 },
  { wasSuccessful: false, error: 4 }
]

但是请注意我的console.log输出..即使我得到正确的结果,它也会显示实际的执行顺序:

time counter:  0
time counter:  1
resolve(4)
time counter:  2
resolve(2)
resolve(3)
time counter:  3
time counter:  4
reject(1)
results_:  [
  { wasSuccessful: false, error: 1 },   // note the array ordering is correct. rejected,
  { wasSuccessful: true, result: 2 },    // resolved,
  { wasSuccessful: true, result: 3 },   // resolved,
  { wasSuccessful: false, error: 4 }    // rejected. good.
]
time counter:  5
time counter:  6
time counter:  7

基本上,此诺言是并行发出的,无论超时时间越快,解决的速度都越快。

相反,我希望它像这样:

time counter:  0
time counter:  1
time counter:  2
time counter:  3
time counter:  4
time counter:  5
reject(1)
time counter:  6
time counter:  7
time counter:  8
resolve(2)
time counter:  9
time counter:  10
time counter:  11
resolve(3)
time counter:  12
resolve(4)
results_:  [
  { wasSuccessful: false, error: 1 },
  { wasSuccessful: true, result: 2 },
  { wasSuccessful: true, result: 3 },
  { wasSuccessful: false, error: 4 }
]
time counter:  13
time counter:  14
...

这是简化。在实践中,我拥有30k +条记录的列表-在该记录上,我需要执行一些api动作,并从本质上解决承诺。我将此列表分组为每个包含10个元素的子列表。我要并行运行每个子列表。

但是大列表..又名列表列表..需要顺序:

bigList = [ [ small parallel list 0 ], [ small parallel list 1 ] .. ]

在这个并行的诺言中,每个诺言已经非常消耗计算量。我很幸运,如果我能并行运行10个。这就是为什么大清单必须是连续的。否则它将发射一棵有3万片叶子的应许树,这将使某些东西崩溃。

仍然不确定在这个规模上是否现实,但是在我执行此顺序之后,我将可以肯定地说出来。

那么如何依次执行上述4个诺言?

谢谢。

我的实际代码非常复杂,但是我会尽我所能简化此操作:let counter = 0 console.log(“ time counter:”,counter)setInterval(()=> {counter = counter + 1; console。日志(” ...

回答如下:

所有这些诺言同时开始,因此您正在并行运行它们

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论