如何在 fetch() 中的 Promise 中执行重定向

How to perform a redirect inside a Promise inside a fetch()

这是在 Promise 内部重定向的正确方法吗?这会造成任何不良影响吗?可能的内存泄漏?

      .then(function (response ){
            if( response.redirected){
              location.href = response.url;
            }
            return response.json();
        })
        .then(function (record) {
            console.log("record update:",record);
                onRecordUpdate(record);
        })
    .then(function (response ){
      if( response.redirected){
        location.href = response.url;
      }
      return response.json();
    })
    .then(function (record) {
      console.log("record update:",record);
      onRecordUpdate(record);
    })

基本上不会运行位置变化后的代码。如果浏览器变慢,有时它可以工作,但理论上它不应该工作。所以我认为你应该在位置更改之前 运行 onRecordUpdate(record) 函数以避免任何静默错误。

正如@nils 所说,您不会发生任何内存泄漏,因为它会刷新页面并清除状态。