javascript - react isomirphic-fetch promise chaining -
okay, understood promise chain works keep returning promise inside previous promise response function:
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); });
if of promise returning function remove chain no continue. , yet today proved me wrong when did in react isomorphic-fetch.
inside auth service;
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 crazy'); }); return promise; },
inside app:
login(){ auth.login(this.state.email, this.state.password).then(() => { notificationmanager.success('successful login'); }).catch((err) => { notificationmanager.error(err.message); }); }
in case of error (if response not json) token should not saved because res.json() function not returned, causing chain stop. yet line "it crazy" printed out, why?
Comments
Post a Comment