簡體   English   中英

Promise。所有promise參數

[英]Promise.All promise parameters

我有兩個承諾,每個承諾都返回一個字符串數組。 我使用Promise.all(p1, p2)運行它們,但令我感到驚訝的是它解析為的values參數是一個12k字符串數組(這將是兩個promise的返回之一)。

const p1 = ModelA.find()
  .then((bandProfiles) => {
    const bandProfilePlayerTags = []
    // [...] Filling this array with strings
    return bandProfilePlayerTags
  })

const p2 = ModelB.find()
  .then((response) => {
    const playerTags = []
    // [...] Filling this array with strings
    return playerTags
  })

Promise.all(p1, p2).then((values) => {
  // Values is an array containing more than 12k strings
})

我期望values是一個長度為2的數組values[0]是諾言1的返回數組,而values[1]是諾言2的返回數組。我在這里缺少什么?

嘗試在數組中傳遞p1和p2

const p1 = ModelA.find()
  .then((bandProfiles) => {
    const bandProfilePlayerTags = []
    // [...] Filling this array with strings
    return bandProfilePlayerTags
  })

const p2 = ModelB.find()
  .then((response) => {
    const playerTags = []
    // [...] Filling this array with strings
    return playerTags
  })

Promise.all([p1, p2]).then((values) => {
  // Values is an array containing more than 12k strings
})

Promise.all(可迭代); 期望一個可迭代的對象作為參數

您確實向Promise.all傳遞了一個(承諾)12k字符串數組,而不是兩個諾言的數組。 您將要使用

Promise.all([p1, p2]).then(values => …)
//          ^      ^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM