【问题标题】:Using Promise on map Function在 map 函数上使用 Promise
【发布时间】:2020-02-27 23:08:42
【问题描述】:

我有一个数组,我想在这个数组上使用 Promise:

let check_duplicate_phone  = store.state.lines.map(function (item) {
    return core.checkIsPhoneDuplicate(item.phone , item.phone_significant);
});

let res  = await Promise.all(check_duplicate_phone).then(function(values) {
    console.log(values);
});

这就是checkIsPhoneDuplicate 函数:

async checkIsPhoneDuplicate(phone , phone_significant) {
    var data = {
        'phone': phone ,
        'phone_significant' : phone_significant
    }
    let res = await Axios.post(checkPhoneDuplicate_route, data)
        .then(response => {                    
            return response.data;
        }).catch(error => console.log(error));
    return res;
}

但它不等待响应,只运行下一步。 我知道它应该曾经喜欢 this 并且我已经阅读了这个 answer 但我没有发现我的错误在哪里。

谢谢。

【问题讨论】:

  • 一些不起作用的是res 总是undefined,因为这是记录valuesthen 回调的返回值。你最好写const values = await Promise.all(check_duplicate_phone); console.log(values);

标签: javascript promise


【解决方案1】:

编辑:删除了不正确的解释,正如 cmets 中所指出的那样。这种方法确实有效,但没有解决 OP 的要求。根据 cmets,OPs 代码也应该像此代码一样工作,因此可能会遗漏一些内容。


function checkIsPhoneDuplicate(phone, phone_significant) {
  var data = {
    phone: phone,
    phone_significant: phone_significant
  };

  return Axios.post(checkPhoneDuplicate_route, data)
    .then(response => response.data)
    .catch(error => console.log(error));
}

async function runCheck() {
  let check_duplicate_phone = store.state.lines.map(function(item) {
    return core.checkIsPhoneDuplicate(item.phone, item.phone_significant);
  });

  let res = await Promise.all(check_duplicate_phone).then(function(values) {
    console.log(values);
  });
}

【讨论】:

  • 它是异步的.. 它让你返回一个承诺。
  • 尽量不要使用 Promise.all(优化问题)。使用:$.when(promise1()).then((result1) => $.when(promise2().then((result2) => { ... });
  • 这个答案可能有效,但解释是错误的,原始代码有什么问题根本不正确。
  • @jfriend00 你能在新答案中分享 OPs 代码中的实际解释吗?
  • OP 的代码没有问题,如图所示。他们的代码应该和你的代码一样好用。通过直接返回承诺,您稍微简化了一些,但两者都应该工作。您的回答没有解释为什么他们的代码不起作用。我认为 OP 可能没有在他们的代码中向我们透露其他一些东西,这才是真正问题的根源。
【解决方案2】:

我不能 100% 确定您的具体问题是什么,但让我帮助您解决这个问题。

async function someFunc() {
  // Obviously this has to be inside of an async function if using await, so let's add it for clarity

  let check_duplicate_phone = store.state.lines.map(function(item) {
    return core.checkIsPhoneDuplicate(item.phone, item.phone_significant);
  });
  /*
    So with this code, you won't get anything in res. You're awaiting the reuslt of
    the promise, which is great, but the .then is going to be called first, 
    and you return nothing from it, so therefore, you won't get anything back
  */
  //let res = await Promise.all(check_duplicate_phone).then(function(values) {
    //console.log(values);
  //});
  
  // try this
  
  let resolvedValues = await Promise.all(check_duplicate_phone); // Values in here
  
  console.log(resolvedValues); // Or however you want to log them.

}




async function checkIsPhoneDuplicate(phone, phone_significant) {
  var data = {
    'phone': phone,
    'phone_significant': phone_significant
  }
  /*
    Again, here you are mixing async/await and .then, which is okay, but
    Let's take full advantage of async/await to clean it up
  */
  /*let res = await Axios.post(checkPhoneDuplicate_route, data)
    .then(response => {
      return response.data;
    }).catch(error => console.log(error));*/
    try {
      let res = await Axious.post(checkPhoneDuplicate_route, data);
      return res.data;
    } catch (e) {
      console.log(e);
    }
}

希望这会有所帮助。我认为这对你有用,或者至少让你走上正确的道路,代码更简洁。

【讨论】:

    猜你喜欢
    • 2018-10-30
    • 2021-12-30
    • 2022-01-18
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多