好吧,正如我所理解的那样,当你在前一个承诺响应函数中继续返回promise时,promise链就会起作用:

SomePromise.then(function(response){
  return anotherPromise(response.data); <- Promise returning function
}).then(function(response){
  return yetAnotherPromise(response.data); <- Promise returning function
}).then(function(response){
   ItIsDone(response.data);
}).catch(function(err){
  someErrorHandling(err);
});

如果任何承诺返回函数将被删除链将不会继续 . 然而今天,当我对同构取法做出反应时,它证明了我的错误 .

内部认证服务;

login: function (email, password) {

    let promise = fetch('http://localhost:3000/auth/login', {
        method : 'POST',
        body   : JSON.stringify({email, password}),
        headers: {
            'Content-Type': 'application/json'
        }
    });

    promise.then(function (res) {
        if (validator.IsJson(res))
            return res.json();
    }).then(function (parsedData) {
        if (parsedData.access_token !== undefined)
            localStorage.setItem('access_token', parsedData);
    }).then(function () {
        console.log('it is crazy');
    });

    return promise;
},

内部应用:

login(){
    auth.login(this.state.email, this.state.password).then(() => {
        NotificationManager.success('Successful login');
    }).catch((err) => {
        NotificationManager.error(err.message);
    });
}

如果出现错误(如果响应不是json),则不应保存令牌,因为未返回res.json()函数,从而导致链停止 . 然而,“它很疯狂”这句话总是打印出来,为什么?