How can I fetch an array of URLs with Promise.all?

Yes, Promise.all is the right approach, but you actually need it twice if you want to first fetch all urls and then get all texts from them (which again are promises for the body of the response). So you’d need to do Promise.all(urls.map(u=>fetch(u))).then(responses => Promise.all(responses.map(res => res.text())) ).then(texts => { … }) Your current code … Read more

Are JavaScript ES6 Classes of any use with asynchronous code bases?

Can I do async constructor() No, that’s a syntax error – just like constructor* (). A constructor is a method that doesn’t return anything (no promise, no generator), it only initialises the instance. And, if not how should a constructor work that does this Such a constructor should not exist at all, see Is it … Read more

fromPromise does not exist on type Observable

UPDATE: As of rxjs 6.0.0-beta.3, operators and observable creators should be imported from rxjs. Furthermore, fromPromise is not part of the public API anymore and its wrapped in the from method. TL;DR; UPDATE For rxjs 6.0.0 use: import { from } from ‘rxjs’; var observableFromPromise = from(promiseSrc); UPDATE: After the release of the pipeable operators … Read more

Filtering an array with a function that returns a promise

Here is a 2017 elegant solution using async/await : Very straightforward usage: const results = await filter(myArray, async num => { await doAsyncStuff() return num > 2 }) The helper function (copy this into your web page): async function filter(arr, callback) { const fail = Symbol() return (await Promise.all(arr.map(async item => (await callback(item)) ? item … Read more

When is the body of a Promise executed?

Immediately, yes, by specification. From the MDN: The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promise constructor even returns the created object) This is defined in the ECMAScript specification (of course, it’s harder to read…) here (Step 9 as of this edit, showing … Read more

What happens if you don’t resolve or reject a promise?

A promise is just an object with properties in Javascript. There’s no magic to it. So failing to resolve or reject a promise just fails to ever change the state from “pending” to anything else. This doesn’t cause any fundamental problem in Javascript because a promise is just a regular Javascript object. The promise will … Read more

Creating a (ES6) promise without starting to resolve it

Good question! The resolver passed to the promise constructor intentionally runs synchronous in order to support this use case: var deferreds = []; var p = new Promise(function(resolve, reject){ deferreds.push({resolve: resolve, reject: reject}); }); Then, at some later point in time: deferreds[0].resolve(“Hello”); // resolve the promise with “Hello” The reason the promise constructor is given … Read more

Intellij Idea warning – “Promise returned is ignored” with aysnc/await

The userController.login() function returns a promise, but you’re not doing anything with the result from the promise by utilizing its then() function. For example: userController.login(req, res).then(() => { // Do something after login is successful. }); or in the ES2017 syntax: await userController.login(req, res); If you don’t actually want to do anything there, I guess … Read more

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