javascript - Why my promise cant resolve? -
i have got 2 promises. 1 not being resolved , dont know why.
processtradeoffer: chain of promises tries procced object called 'offer'. identifyoffer return var "valida" | "aceptable" | "denegable". if 'valida' need identify items offer. need async function identifyitems(offer) return var 'offerstate' "denegable" | "aceptable" , can decline or accept offer.
i know problem there not in statement (offerstate == 'valida'). code:
const processtradeoffer = function(offer) { return new promise(function(resolve, reject) { identyoffer(offer) .then(function(offerstate) { return finishtradeoffer(offer, offerstate); }).then(function() { console.log('aqui'); return resolve(); }) }) } const finishtradeoffer = function(offer, offerstate) { return new promise(function(resolve, reject) { if (offerstate == 'aceptable') { accepttradeoffer(offer).then(function() { return resolve(); }) } else if (offerstate == 'denegable') { declinetradeoffer(offer).then(function() { console.log('here'); return resolve(); }) } else if (offerstate == 'valida') { identifyitems(offer).then(function(offerstate) { finishtradeoffer(offer, offerstate); }) } }) }
console.log('here') fired succesfully , console.log('aqui') dont.
please write code follows.
const processtradeoffer = function(offer) { const result = identyoffer(offer) .then(offerstate => finishtradeoffer(offer, offerstate)) result.then(() => console.log('aqui')); return result; }; const finishtradeoffer = function(offer, offerstate) { switch(offerstate) { case 'aceptable': return accepttradeoffer(offer); case 'denegable': { const result = declinetradeoffer(offer); result.then(() => console.log('here')); return result; case 'valida': return identifyitems(offer) .then(offerstate => finishtradeoffer(offer, offerstate)); default: throw "invalid value offerstate!!"; } };
the basic point handle case of valida
in such way promise resolves. in addition, we've gotten rid of "explicit promise constructor anti-pattern".
Comments
Post a Comment