nested promise typescript angular 2 -
i have method fallbacktolocaldbfileorlocalstoragedb
return promise , calls method getdbfilexhr
promise.
in code, know have use 'resolve()' or if resolving getdbfilexhr
automatically resolve fallbacktolocaldbfileorlocalstoragedb
?
as can see commented then().catch()
parts, don't know if have leave them or not.
thanks help.
fallbacktolocaldbfileorlocalstoragedb() { return new promise(function (resolve, reject) { if (this.storageservice.get('prodata') === null) { if (this.connectionstatus.f() !== 'online') { } else { this.senderroremail("bl: online falling local prodb", 10); } console.log('...falling local probd.jsonp.'); return this.getdbfilexhr('prodb.jsonp'); // .then(function () { // console.log('...falling local probd.jsonp succeeded.'); // resolve(); // }) // .catch(, function () { // console.log('...error, shit.'); // reject(); // });
edit showing full nested functions, partially fixed code:
import { injectable } '@angular/core'; ... export class updateprodb { constructor( ) { } get() { var debugoptionuselocaldb = 0, prodata = [], serverattempts = 0; return new promise((resolve, reject) => { if (debugoptionuselocaldb) { return this.fallbacktolocaldbfileorlocalstoragedb(); } if (this.connectionstatus.f() === 'online') { console.log("fetching db server:"); return this.getdbfilexhr(this.dburl(), serverattempts) .then(function (data) { console.log('-normal xhr request succeeded.'); resolve(data); }) .catch((reason)=> { if (typeof serverattempts !== "undefined") serverattempts++; console.log('on passe dans le catch, serverattempts = ', serverattempts) if (serverattempts < 2) { return this.getdbfilexhr(this.dburl(), serverattempts) .then(function () { console.log('-basic xhr request succeeded.'); }) .catch(function (){ console.log("-basic xhr request failed, falling local db file or localstorage db..."); }) } else { console.log("-basic xhr request failed, falling local db file or localstorage db..."); return this.fallbacktolocaldbfileorlocalstoragedb() .then((data)=>{ resolve(data); }) .catch((reason)=> { reject(reason); }); } }); }); } getdbfilexhr(url, serverattempts) { return new promise((resolve, reject) => { var request = new xmlhttprequest(); request.open("get", url, true); request.onload = ()=> { if ( (request.readystate === 4) && ( (request.status >= 200 && request.status <= 299) || request.status === 304 || request.status === 0) ) { console.log('-we response '+request.status+' xhr in getdbfilexhr'); var jsontext = request.responsetext.replace("callback(", "").replace(");", ""); if (jsontext === '') { console.error('-error'); reject({ status: request.status, statustext: request.statustext }); } else { var parsedjson; try { parsedjson = json.parse(jsontext); } catch (e) { resolve(request.response); } } }; request.onerror = ()=> { reject({ status: request.status, statustext: request.statustext }); }; console.log("sending request.send()"); request.send(); }); } fallbacktolocaldbfileorlocalstoragedb() { return new promise((resolve, reject) => { if (this.storageservice.get('prodata') === null) { if (this.connectionstatus.f() !== 'online') { } else { this.senderroremail("bl: online falling local prodb", 10); } console.log('...falling local probd.jsonp.'); return this.getdbfilexhr('prodb.jsonp', undefined) .then(function (data) { console.log('...falling local probd.jsonp succeeded.'); resolve(data); }) .catch((reason)=> { console.log('...error, shit.'); reject(reason); }); } else { resolve(); } }); }
the argument new promise()
called executor function. needs either resolve()
or reject()
promise.
what trying return promise executor function. according mdn, "the return value of executor ignored."
this means way you're trying use inner promise
won't work, , must explicitly resolve or reject you're doing in commented code.
Comments
Post a Comment