How to return a proper Promise with TypeScript

It is considered a good practice to embed the whole function body inside the Promise constructor, so should any error happen, it would be converted to a rejection. In this case it solves your problem too I believe. saveMyClass(updatedMyClass: MyClass) { return new Promise<Package>((resolve, reject) => { //saving MyClass using http service //return the saved … Read more

How to check if a Promise is pending [duplicate]

You can attach a then handler that sets a done flag on the promise (or the RunTest instance if you prefer), and test that: if (!this.promise) { this.promise = this.someTest(); this.promise.catch(() => {}).then(() => { this.promise.done = true; }); retVal = true; } if ( this.promise.done ) { this.promise = this.someTest(); this.promise.catch(() => {}).then(() => … Read more

Catch all unhandled javascript promise rejections

The whole world is waiting for the unhandledrejection and rejectionhandled events. As of March 2016, Chrome is now the first to support it. Example: window.addEventListener(‘unhandledrejection’, function(event) { console.error(‘Unhandled rejection (promise: ‘, event.promise, ‘, reason: ‘, event.reason, ‘).’); }); Specification: HTML Living Standard Mozilla Developer: onrejectionhandled, onunhandledrejection Chromium Issues: 495801, 393913

using a fetch inside another fetch in javascript

Fetch returns a promise, and you can chain multiple promises, and use the result of the 1st request in the 2nd request, and so on. This example uses the SpaceX API to get the info of the latest launch, find the rocket’s id, and fetch the rocket’s info. const url=”https://api.spacexdata.com/v4″; const result = fetch(`${url}/launches/latest`, { … Read more

Unhandled rejections in Express applications

Putting the unhandledRejection inside a middleware…often results in a Error: Can’t render headers after they are sent to the client. Make a slight change to your error handler: // production error handler const HTTP_SERVER_ERROR = 500; app.use(function(err, req, res, next) { if (res.headersSent) { return next(err); } return res.status(err.status || HTTP_SERVER_ERROR).render(‘500’); }); From the ExpressJS … Read more

fetch: Reject promise with JSON error object

// This does not work, since the Promise returned by `json()` is never fulfilled return Promise.reject(resp.json()); Well, the resp.json promise will be fulfilled, only Promise.reject doesn’t wait for it and immediately rejects with a promise. I’ll assume that you rather want to do the following: fetch(url).then((resp) => { let json = resp.json(); // there’s always … Read more

What do double brackets mean in javascript and how to access them

What’s the stuff inside [[]] My question is what do the double brackets [[ ]] mean, and how do I retrieve the value of [[PromiseValue]]. It’s an internal property. You cannot access it directly. Native promises may only be unwrapped in then with promises or asynchronously in generally – see How to return the response … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)