Why does javascript ES6 Promises continue execution after a resolve?

JavaScript has the concept of “run to completion”. Unless an error is thrown, a function is executed until a return statement or its end is reached. Other code outside of the function can’t interfere with that (unless, again, an error is thrown). If you want resolve() to exit your initializer function, you have to prepend … Read more

How can I export promise result?

You could easily assign it to an exported variable, but you should not do that – the assignment happens asynchronously, and the variable might be read before that in the modules where it is imported. So instead, just export the promise1! // data.js import urls from ‘./urls’ import getData from ‘./get-data’ export default getData(urls).then(responses => … Read more

ES6 Promise.all progress

I’ve knocked up a little helper function that you can re-use. Basically pass your promises as normal, and provide a callback to do what you want with the progress.. function allProgress(proms, progress_cb) { let d = 0; progress_cb(0); for (const p of proms) { p.then(()=> { d ++; progress_cb( (d * 100) / proms.length ); … 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

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

Promise and Promise.all(array) executed before array fulfilled [duplicate]

As georg said, you’re just not putting the promises in arr, because you never return anything from the anonymous function you’ve wrapped around new Promise. That function is also completely unnecessary, so: var arr = []; for (var i = 0; i < 2; i++) { arr.push(new Promise(function(resolve, reject) { setTimeout(function() { log(‘resolved !’); resolve(); … Read more