es6-promise

Using Fetch API with Promise.all

妖精的绣舞 提交于 2019-12-06 12:39:03
问题 my aim is to fetch data from two URLs and perform an action only when both have come back successfully. On the other hand i want to return an error if either of them fail. I have played around with my code and managed to get the desired effect. My question is, is there a more efficient, succinct way of achieving the same functionality? Helper functions let status = (r) => { if (r.ok) { return Promise.resolve(r) } else { return Promise.reject(new Error(r.statusText)) } } let json = (r) => r

Javascript Promise push value into array (only from function or outside?)

大兔子大兔子 提交于 2019-12-06 12:08:44
问题 I have some promises and a Promise.all: array = []; var one = new Promise(function(resolve, reject) { // Do stuff setTimeout(function() { resolve('One Done'); array.push('one'); }, 5000); }); var two = new Promise(function(resolve, reject) { // Do Stuff resolve('Two Done'); array.push('two'); }); Promise.all(array).then(values => { console.log(values); }); We know this doesn't work because array.push needs to be outside. I currently have a few functions which I need to have called by promises

JavaScript async/await for Promises inside Array.map() [duplicate]

旧时模样 提交于 2019-12-06 11:45:26
问题 This question already has answers here : Using async/await with a forEach loop (17 answers) Closed 3 years ago . Given the following code class SomeClass { async someFunc() { const urlParameters = [0, 1, 2]; const batchAjaxResult = await urlParameters.map((parameter) => { const result = await $.get(`someUrl/${parameter}`); return { parameter, result } }); console.log(batchAjaxResult); } } JavaScript will return an Array of resolved Promises instead of the actual Promises result. This is

Angular 2 chained Promise and passing on reject

坚强是说给别人听的谎言 提交于 2019-12-06 11:28:11
Should be a simple question, however I can find no documentation on how to do it. Chaining a promise like this: // Making a promise, no problem let promise = new Promise((resolve, reject) => { let data = {}; // do something reject(data); }); // First chain, no problem promise.then(data => { console.log('Success callback', data); // This will not be called }).catch(err => { console.log('Error callback', err); // This WILL be called }) // Second chain, acts as resolved .then(data => { console.log('Level 2 success', data); // This WILL be called }).catch(err => { console.log('Level 2 error', err)

Return Promise from activate() when customElements have loaded

巧了我就是萌 提交于 2019-12-06 08:53:04
问题 I know that Aurelia allows us to return a Promise() from the VM's activate() method, and if so it'll wait for the promise to resolve before switching to the new view. But, say I have a view that consists of one or several child components and they all make HTTP requests, how can I know when all children are finished from within my parent component? Bonus question: Is it correct that only VM's that go in the <router-outlet> utilize the activate() method, whereas VM's that are used as custom

How do I make ES6 generators wait for promises, like in redux-saga?

穿精又带淫゛_ 提交于 2019-12-06 08:17:59
I've read that generators don't wait for promises. How come this is not the case with generators in redux-saga , and how do I make my own generators wait? For example, this saga: takeLatest('FETCH_USER_REQUESTED', function*() { const fetchPromise = yield put(fetchUser()); const user = yield fetchPromise; console.log(user) yield 1 console.log(1) }) will output: Promise Object // <= user data fetched asynchronously 1 instead of: Promise undefined 1 How come this is not the case with generators in redux-saga, and how do I make my own generators wait? This very popular belief, however generators

How does .then(console.log()) and .then(() => console.log()) in a promise chain differ in execution

左心房为你撑大大i 提交于 2019-12-06 08:08:29
Is there any difference in efficiency? Will the behavior be any different if a setTimeout is used instead of console.log() You can basically do these three things .then(console.log()) This calls the console.log immediately, without waiting until the promise is resolved, so it is not probably something that you would want to do. .then(console.log) This executes the console.log only after the promise has successfully resolved (requires one function call) and implicitly pass the result of the promise to to the console.log function. .then(() => console.log()) Same as before, requires 2 function

Javascript: Run async task in series(or sequence) without libraries

帅比萌擦擦* 提交于 2019-12-06 06:17:15
问题 I want to run some asynchronous task in a loop, but it should execute in sequence order(one after another). It should be vanilla JS, not with any libraries. var doSome = function(i) { return Promise.resolve(setTimeout(() => { console.log('done... ' + i) }, 1000 * (i%3))); } var looper = function() { var p = Promise.resolve(); [1,2,3].forEach((n) => { p = p.then(() => doSome(n)) }) return p; } looper(); Current output: calling for ...1 calling for ...2 calling for ...3 Promise {<resolved>:

I can't get the value of “result” in Node.js

痞子三分冷 提交于 2019-12-06 04:57:45
In my console.log(info) , I want to get the value of "result". But when I use console.log(info) , I get Promise { <pending> } : var info=new Promise((resolve, reject) => { request(options, (err, res, body) => { if (body.match('success') || body.match('code="25"')) { resolve("success"); } else { reject(body); } }); }).then(result => { return result; }).catch(err => { console.log("error: " + err) }); console.log(info); I would like to get info == result . How can I do it? Thanks What happens here First some explanation of what happens here. When you do something like this: var info = new Promise

Retry on Javascript.Promise.reject a limited number of times or until success

两盒软妹~` 提交于 2019-12-06 04:24:27
问题 I have a function say myMainFunction that is called from a client, that in turn calls mypromisified function. Scenario: mypromisified function can fail intermittently and I need to call this function with a delay (at an exponential increase) until success or until max no of tries reached. What I have so far The following code illustrates my scenario and repeats itself until success, but it tries indefinitely and not until certain count is reached // called once from the client myMainFuntion()