fs.writeFile in a promise, asynchronous-synchronous stuff

As of 2019… …the correct answer is to use async/await with the native fs promises module included in node. Upgrade to Node.js 10 or 11 (already supported by major cloud providers) and do this: const fs = require(‘fs’).promises; // This must run inside a function marked `async`: const file = await fs.readFile(‘filename.txt’, ‘utf8’); await fs.writeFile(‘filename.txt’, … Read more

how to cancel/abort ajax request in axios

Axios does not support canceling requests at the moment. Please see this issue for details. UPDATE: Cancellation support was added in axios v0.15. EDIT: The axios cancel token API is based on the withdrawn cancelable promises proposal. UPDATE 2022: Starting from v0.22.0 Axios supports AbortController to cancel requests in fetch API way: Example: const controller … Read more

How to use Typescript with native ES6 Promises

The current lib.d.ts doesn’t have promises in it defined so you need a extra definition file for it that is why you are getting compilation errors. You could for example use (like @elclanrs says) use the es6-promise package with the definition file from DefinitelyTyped: es6-promise definition You can then use it like this: var p … Read more

How to return many Promises and wait for them all before doing other stuff

You can use Promise.all (spec, MDN) for that: It accepts a bunch of individual promises and gives you back a single promise that is resolved when all of the ones you gave it are resolved, or rejected when any of them is rejected. So if you make doSomeAsyncStuff return a promise, then: const promises = … Read more

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

Why can I not throw inside a Promise.catch handler?

As others have explained, the “black hole” is because throwing inside a .catch continues the chain with a rejected promise, and you have no more catches, leading to an unterminated chain, which swallows errors (bad!) Add one more catch to see what’s happening: do1().then(do2).catch(function(err) { //console.log(err.stack); // This is the only way to see the … Read more

How to pass parameter to a promise function

Wrap your Promise inside a function or it will start to do its job right away. Plus, you can pass parameters to the function: var some_function = function (username, password) { return new Promise(function (resolve, reject) { /* stuff using username, password */ if (/* everything turned out fine */) { resolve(“Stuff worked!”); } else … Read more

What is the best way to limit concurrency when using ES6’s Promise.all()?

P-Limit I have compared promise concurrency limitation with a custom script, bluebird, es6-promise-pool, and p-limit. I believe that p-limit has the most simple, stripped down implementation for this need. See their documentation. Requirements To be compatible with async in example ECMAScript 2017 (version 8) Node version > 8.2.1 My Example In this example, we need … Read more

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