如何突破 promise (.then) 语句 javascript

How to break out of a promise (.then) statement javascript

当 catch 语句中发生错误时,我在尝试中断 promise 语句时遇到问题。

我不确定是否可以在 catch 语句中抛出错误。

问题:当我抛出错误时,catch 函数没有做任何事情。

预期结果:catch 语句显示警报并中断承诺链。

代码

        if (IsEmail(email)) {
        $('body').loadingModal({
              position: 'auto',
              text: 'Signing you in, please wait...',
              color: '#fff',
              opacity: '0.9',
              backgroundColor: 'rgb(0,0,0)',
              animation: 'doubleBounce'
        });

        var delay = function(ms){ return new Promise(function(r) { setTimeout(r, ms) }) };
        var time = 2000;

        delay(time)
        .then(function() { $('body').loadingModal('animation', 'foldingCube'); return delay(time); } )
        .then(function() { 
            firebase.auth().signInWithEmailAndPassword(email, password)
            .then(function () {
                var user = firebase.auth().currentUser;
                uid = user.uid;
                configure();
            })
            .catch(function(error) {
                throw error;
            });
        })
        .then(function() { $('body').loadingModal('color', 'white').loadingModal('text', 'Welcome to Dtt deliveries').loadingModal('backgroundColor', 'orange');  return delay(time); } )
        .then(function() { $('body').loadingModal('hide'); return delay(time); } )
        .then(function() { $('body').loadingModal('destroy') ;} )
        .catch(function(error) {
            alert("Database error: " + error);
        }); 
    }
    else {
        alert("Please enter a valid email");
        return;
    }

delay 之后的第二个 .then 立即解决,因为没有任何内容被 return 编辑。 Return signInWithEmailAndPassword 调用,因为它 return 是一个 Promise,您需要将其与外部 Promise 链链接在一起:

.then(function() {
  return firebase.auth().signInWithEmailAndPassword(email, password)
  // ...

此外,捕获并立即抛出并没有真正做任何事情 - 除非你 需要 处理那里 signInWithEmailAndPassword 特有的错误,请随意忽略 catch完全:

delay(time)
  .then(function() { $('body').loadingModal('animation', 'foldingCube'); return delay(time); } )
  .then(function() { 
    return firebase.auth().signInWithEmailAndPassword(email, password)
  })
  .then(function () {
    var user = firebase.auth().currentUser;
    uid = user.uid;
    configure(); // if configure returns a Promise, return this call from the `.then`
  })
  .then(
  // ...
  .catch(function(error) {
    alert("Database error: " + error);
  });

如果 configure return 也是一个 Promise,那么您也需要 return 它。 (如果是同步的,就不需要)

(您也可以考虑使用更 user-friendly 的方式来显示错误,也许使用适当的模式而不是 alert

另一个要考虑的选项是使用 await 而不是所有这些 .then,控制流程可能更清晰:

(async () => {
  if (!IsEmail(email)) {
    alert("Please enter a valid email");
    return;
  }
  $('body').loadingModal({
    position: 'auto',
    text: 'Signing you in, please wait...',
    color: '#fff',
    opacity: '0.9',
    backgroundColor: 'rgb(0,0,0)',
    animation: 'doubleBounce'
  });

  var delay = function(ms) {
    return new Promise(function(r) {
      setTimeout(r, ms)
    })
  };
  var time = 2000;

  try {
    await delay(time);
    $('body').loadingModal('animation', 'foldingCube');
    await delay(time);
    await firebase.auth().signInWithEmailAndPassword(email, password)
    var user = firebase.auth().currentUser;
    uid = user.uid;
    configure(); // if this returns a Promise, `await` it
    $('body').loadingModal('color', 'white').loadingModal('text', 'Welcome to Dtt deliveries').loadingModal('backgroundColor', 'orange');
    await delay(time);
    $('body').loadingModal('hide');
    await delay(time);
    $('body').loadingModal('destroy');
  } catch(error) {
    alert("Database error: " + error);
  }
})();