javascript - Uncaught exception onReject callback Promise -
since yesterday , try understand behavior did not know onreject promise callback. in fact, work api (return json data) , superagent make request. have class:
class api { constructor(uri) { ... } // simple http request get(...) { var url = this.buildurl(...); this.requestpending = request.get(url); return new promise((resolve, reject) => { this.requestpending.end((err, res) => { if (err) { reject(res.body.error); } else { resolve(res.body); } }); }); } }
what not understand that:
reject(res.body.error);
throw exception (res.body.error contains simple string). indeed, firebug display error:
uncaught exception: ...
could explain behavior ?
ps: work babel transpile js.
edit
i tested when librairie have same result. however, when write:
reject(new error("foo"));
the onreject callback never call:
when.all([ api.get(...), api.get(...) ]).then(data => { console.log(data) }, err => { console.log(err); })
and in browser:
i continue research.
edit 2
really sorry , deserve whipped on public square !!!. in fact, had change 1 file instead of two. really, sorry !!
i dont know whether silly or not,may scope requestpending might problem.sorry if sounds silly,
var url = this.buildurl(...),self = this; this.requestpending = request.get(url); return new promise((resolve, reject) => { self.requestpending.end((err, res) => { if (err) { reject(res.body.error); } else { resolve(res.body); } }); }); } }
Comments
Post a Comment