es6-promise

Can I Transpile for ES6-ES5 without npm, VS, Node, etc. and just the JS code itself somehow?

那年仲夏 提交于 2019-12-21 05:18:06
问题 I am trying to get Firefox to run a Promise in ES6 but run into the 'let' keyword triggering an error; SyntaxError: let is a reserved identifier Changing the script tag to include; type="application/javascript;version=1.7" did not work, so I am seeking to Transpile the code. My situation is that there is nothing being used except a text editor. No NPM, not Node or Angular, no Visual Studio, nothing. So when I investigated the Compilers, I saw no option to let me Transpile this code without

Promise.all find which promise rejected

为君一笑 提交于 2019-12-20 19:48:39
问题 In my code, I am using Promise.all() to run code asynchronously once some promises have all fulfilled. Sometimes, one promise will fail, and I'm not sure why. I would like to know which promise is failing. Passing a callback as a second parameter to the .then method does not help much, as I know that a promise is rejecting but not which promise is rejecting. A stack trace does not help either, as the first item is the Promise.all() 's error handler. A line number from the Error object passed

Implementing Promise.series as alternative to Promise.all

谁说我不能喝 提交于 2019-12-20 10:46:19
问题 I saw this example implementation of Promise.all - which runs all promises in parallel - Implementing Promise.all Note that the functionality I am looking for is akin to Bluebird's Promise.mapSeries http://bluebirdjs.com/docs/api/mapseries.html I am making an attempt at creating Promise.series, I have this which seems to work as intended ( it actually is totally wrong, don't use it, see answers ): Promise.series = function series(promises){ return new Promise(function(resolve,reject){ const

How do you implement a “raceToSuccess” helper, given a list of promises?

那年仲夏 提交于 2019-12-20 09:53:13
问题 I'm puzzled by something in the ES6 Promise API. I can see a clear use case for submitting multiple async jobs concurrently, and "resolving" on the first success. This would, for example, serve a situation where multiple equivalent servers are available, but some are perhaps down, and others heavily loaded and slow, so my goal would be to get a response from the first one to succeed, and ignore the rest (yes, I know this is an obnoxious way for a client to behave from a server's perspective,

What is the point of promises in JavaScript?

筅森魡賤 提交于 2019-12-20 08:06:30
问题 A promise is a (...) value which may be available now, or in the future, or never (Source: MDN) So lets say I have an app which wants to work with pictures. The pictures are loaded e.g. after an algorithm works with it in the background (or some other sort of delay). Now I want to check, if the pictures are available in the future , by using a promise, not a callback. To check, if an image is available, I could use the following code: function loadImage(url) { return new Promise((resolve,

Is there a way detect if a rejected promise is unhandled?

时光毁灭记忆、已成空白 提交于 2019-12-20 04:25:06
问题 Let’s say I have a function foo which returns a promise. Is there a way to call the function, and optionally Promise.prototype.catch the result only if its rejection is unhandled? I want a solution which works in both node.js and the browser. For example: const fooResult = foo(); // pass fooResult somewhere else where fooResult may be caught with catch catchIfUncaught(fooResult, (err) => { console.log(err); // should be foo rejection only if the rejection is not caught elsewhere // no

How to unnest these Promises?

本秂侑毒 提交于 2019-12-20 03:28:44
问题 Background I have a function that makes a request to a server. If the request fails, I want to: 1. log the error 2. run a terminal command 2.1 log if the command failed or succeeded To achieve this I have the following code: const createRequest = ( { request, logger, terminal } ) => ( { endpoint, timeout } ) => request.get( endpoint, { timeout } ) .then( response => logger.info( { event: "Heartbeat request succeeded.", status: response.status } ) ) .catch( err => logger.error( { event:

My implementation of debounce axios request left the promise in pending state forever, is there a better way?

和自甴很熟 提交于 2019-12-20 01:57:11
问题 I need a simple debounce function with immediate always true. Without resorting to lodash and with the help of Can someone explain the "debounce" function in Javascript , I implemented it as following, function debounce(func, wait) { var timeout; return function() { if (!timeout) func.apply(this, arguments); clearTimeout(timeout); timeout = setTimeout(()=>{timeout = null}, wait); }; }; It works as expected until I need to debounce axios request. Assumed I have a debounced axios method, I

Promises in Node.js core

筅森魡賤 提交于 2019-12-19 11:42:54
问题 I hear that Promises are available for Node core APIs. For example with fs, how can we use promises, do we just omit the callback? fs.readFile(file).then(v => console.log(v)); or how do we use? at the moment for Node.js versions older than 10, I am guessing, it's behind a flag? Maybe: node --promises ? 回答1: This experimental feature was added in node v10.0.0. You'll need to require fs/promises instead of fs 回答2: As of now, most Node callback-based APIs should be manually promisified in order

How to include both a parsed response and original response headers in fetch errors [duplicate]

瘦欲@ 提交于 2019-12-19 10:23:50
问题 This question already has answers here : How do I access previous promise results in a .then() chain? (17 answers) Closed 2 years ago . I have the following promise chain: return fetch(request) .then(checkStatus) .then(response => response.json()) .then(json => ({ response: json })) .catch(error => ({ error })) Where checkstatus() checks if the request was successful, and returns an error if it wasn't. This error will be caught and returned. But, the problem is that I want to add the both