【问题标题】:Keep getting an undefined result from new Promise不断从新的 Promise 获得未定义的结果
【发布时间】:2020-10-05 16:01:35
【问题描述】:

const randomIntegerFromInterval = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1) + min);
};

const makeTransaction = (transaction) =>
  new Promise((res, rej) => {
    const delay = randomIntegerFromInterval(200, 500);
    setTimeout(() => {
      const canProcess = Math.random() > 0.3;
      if (canProcess) {
        res(transaction.id, randomIntegerFromInterval(200, 500));
      } else {
        rej(transaction.id);
      }
    }, delay);
  });

const logSuccess = (id, time) => {
  console.log(`Transaction ${id} processed in ${time}ms`);
};

const logError = (id) => {
  console.warn(`Error processing transaction ${id}. Please try again later.`);
};

makeTransaction({ id: 70, amount: 150 }).then(logSuccess).catch(logError);

makeTransaction({ id: 71, amount: 230 }).then(logSuccess).catch(logError);

makeTransaction({ id: 72, amount: 75 }).then(logSuccess).catch(logError);

makeTransaction({ id: 73, amount: 100 }).then(logSuccess).catch(logError);

由于某种原因,我不断得到一个未定义的值。还没弄清楚为什么会这样。

输出必须是这样的。请解释是什么问题,或者我应该改变什么。

task_3.js:23 Error processing transaction 73. Please try again later.
task_3.js:19 Transaction 70 processed in 458ms
task_3.js:19 Transaction 72 processed in 354ms
task_3.js:23 Error processing transaction 71. Please try again later.

【问题讨论】:

标签: javascript promise


【解决方案1】:

像这样解决承诺

res({
  id: transaction.id, 
  time: randomIntegerFromInterval(200, 500)
});

然后像这样记录

const logSuccess = ({id, time}) => {
  console.log(`Transaction ${id} processed in ${time}ms`);
};

【讨论】:

    【解决方案2】:

    这是因为执行器函数回调resrej 只接受一个参数。你需要把它改成这样:

    const randomIntegerFromInterval = (min, max) => {
      return Math.floor(Math.random() * (max - min + 1) + min);
    };
    
    const makeTransaction = (transaction) =>
      new Promise((res, rej) => {
        const delay = randomIntegerFromInterval(200, 500);
        setTimeout(() => {
          const canProcess = Math.random() > 0.3;
          if (canProcess) {
            res({id: transaction.id, time: randomIntegerFromInterval(200, 500)});
          } else {
            rej(transaction.id);
          }
        }, delay);
      });
    
    const logSuccess = ({id, time}) => {
      console.log(`Transaction ${id} processed in ${time}ms`);
    };
    
    const logError = (id) => {
      console.warn(`Error processing transaction ${id}. Please try again later.`);
    };
    
    makeTransaction({ id: 70, amount: 150 }).then(logSuccess).catch(logError);
    
    makeTransaction({ id: 71, amount: 230 }).then(logSuccess).catch(logError);
    
    makeTransaction({ id: 72, amount: 75 }).then(logSuccess).catch(logError);
    
    makeTransaction({ id: 73, amount: 100 }).then(logSuccess).catch(logError);

    有关参考,请参阅this

    这两个函数的签名很简单,它们接受任何类型的单个参数。

    【讨论】:

    • 塞巴斯蒂安,我能问你:'return new Promise' 和像我一样的 'new Promise' 有什么区别?
    • 不同之处在于当你return一个Promise时,你可以链接它(调用func().then().catch())。如果不返回 Promise,您将无法做到这一点。事实是,您也 确实 返回了一个 Promise,因为箭头函数在您的情况下为您执行此操作。这就是为什么您可以在函数调用中使用.then().catch()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2015-05-16
    • 1970-01-01
    • 2017-06-20
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多