Creating a promise chain in a for loop

You have to assign the return value of .then back to chain: chain = chain.then(()=>getProm(i)) .then(Wait) Now you will basically be doing chain .then(()=>getProm(1)) .then(Wait) .then(()=>getProm(2)) .then(Wait) .then(()=>getProm(3)) .then(Wait) // … instead of chain .then(()=>getProm(1)) .then(Wait) chain .then(()=>getProm(2)) .then(Wait) chain .then(()=>getProm(3)) .then(Wait) // … You can see that the first one is actually a chain, … Read more

Is there a version of setTimeout that returns an ES6 promise?

In Browsers First of all no – there is no built in for this. Lots of libraries that enhance ES2015 promises like bluebird whip with it. I think the other answer conflates executing the function and a delay, it also creates timeouts that are impossible to cancel. I’d write it simply as: function delay(ms){ var … Read more

How to determine if a Promise is supported by the browser

Update Dec 11 – 2016: All evergreen versions of browsers now support promises. They are safe to use. Update Nov 14 – 2016: Chrome, Firefox, Safari and IE all now have experimental support for promises in their dev channels. The specification has settled. I would still not rely on the implementation just yet and would … Read more

javascript : Async/await in .replace

An easy function to use and understand for some async replace : async function replaceAsync(str, regex, asyncFn) { const promises = []; str.replace(regex, (match, …args) => { const promise = asyncFn(match, …args); promises.push(promise); }); const data = await Promise.all(promises); return str.replace(regex, () => data.shift()); } It does the replace function twice so watch out if … 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

Best es6 way to get name based results with Promise.all

ES6 supports destructuring, so if you just want to name the results you can write: var myFuncAsync1 = () => Promise.resolve(1); var myFuncAsync2 = () => Promise.resolve(2); Promise.all([myFuncAsync1(), myFuncAsync2()]) .then(([result1, result2]) => console.log(result1 +” and “+ result2)) //1 and 2 .catch(e => console.error(e)); Works in Firefox and Chrome now.

What are the differences between observables and promises in JavaScript?

What is the difference between observables and promises? Simply put: A promise resolves to a single value asynchronously, an observable resolves to (or emits) multiple values asynchronously (over time). Concrete examples: Promise: Response from an Ajax call Observable: Click events More information can be found here: http://reactivex.io/intro.html i’ve read that observables are looking to overtake … Read more

Karma, PhantomJS and es6 Promises

You can pull in the Babel polyfill by simply installing Babel Polyfill: npm install –save-dev babel-polyfill and then include the polyfill file before your source and test files within the files section of your karma.conf.js: files: [ ‘node_modules/babel-polyfill/dist/polyfill.js’, ‘index.js’, //could be /src/**/*.js ‘index.spec.js’ //could be /test/**/*.spec.js ], Unless you know that all your target browsers … Read more

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