es6-promise

Chaining .then() calls in ES6 promises

家住魔仙堡 提交于 2019-12-11 01:53:51
问题 I thought it was supposed to be possible to chain the .then() method when using ES6 Promises. In other words I thought that when a promise is resolved the value passed to the resolve function should be passed to any chained then handlers. If this is so how come value comes back undefined in the chained then handler below? function createPromise() { return new Promise((resolve) => { resolve(true); }); } createPromise() .then((value) => { console.log(value); // expected: true, actual: true })

Promise retries until success/failure with Typescript

家住魔仙堡 提交于 2019-12-11 01:46:13
问题 My mobile app uploads several files to the server in succession, often from remote areas with questionable connection strength. For this reason, I want to make a few attempts to send the file. I also want to move on and attempt the next one in the event of a failure, with all error messages displayed at the end of the export (ie "10 files uploaded, 3 failed...") However, I'm having trouble figuring out the recursive retry pattern with promises. Here's what I have so far: sendFile(params,

Promise for-loop with Ajax requests

感情迁移 提交于 2019-12-11 00:35:32
问题 I'm creating a native JavaScript application which does loads of Ajax calls in the same process at a certain time. Instead of going through a normal for loop and do them all at once, I thought I'd wait for the Ajax call to complete and then do the next one. With the help of Stackoverflow I've managed to do this like the following: function ajaxCall(index) { return new Promise(function(resolve) { // Ajax request { resolve(); // } }); } Promise.resolve(0).then(function loop(i) { if (i < length)

after adding a listener to a Promise, should I use the original promise or the new one?

北城以北 提交于 2019-12-11 00:02:05
问题 I have some javasript code that takes an existing promise (say, the promise returned by fetch()) and adds value (say, then/catch listeners for debugging, or maybe more): let myFetch = function(url) { return fetch(url).then(function(value) { console.log("fetch succeeded: value=",value); return value; }.catch(function(reason) { console.log("fetch failed: reason=",reason); throw reason; }); }; I found myself modifying the above code so that the listeners are added only if some condition is true:

Promise return type error after TypeScript upgrade

こ雲淡風輕ζ 提交于 2019-12-10 22:09:17
问题 Using typescript v2.3.4, the following code worked fine: getStuff() : Promise<IStuff>{ return this.http.get(url, options) .toPromise() .then( (res) => { let stuff: IStuff; if (res.json().data){ stuff = res.json().data; } return stuff; }) .catch( (reason) => { this.handleError(reason); }); } ...where handleError is like this: handleError = (error:any) => { this.debug(error); throw error; }; Now, with typescript v2.4.1 I get the error: 'Type 'Promise<void | IStuff>' is not assignable to type

How to hold a NodeJS application until other promise completes

一曲冷凌霜 提交于 2019-12-10 19:49:56
问题 Using promises with NodeJS, I load a model that can then be re-used by susequent calls to the NodeJS app. How can I prevent the same object/model being loaded twice from a database if a second request arrives while the first is still being loaded? I set a "loading flag" to say that the object is being retrieved from the database and "loaded" when done. If there is a second request that attempts to load the same object, it needs to wait until the initial model is filled and then both can use

call then only after method returning promise is finished

风格不统一 提交于 2019-12-10 19:28:51
问题 submitTCtoDB() { console.log("this.selectedFileList is: " + this.selectedFileList) this.readFile().then(() => { alert("ReadFile Finished now submit TC"); this.submitTC() }); } readFile() { return new Promise((resolve, reject) => { for (let i = 0; i < this.selectedFileList.length; i++) { let file = this.selectedFileList[i]; alert("file in redafile" + file.name) let fileReader = new FileReader(); fileReader.onload = () => { this.fileContent = fileReader.result; if (this.fileContent.indexOf("END

Complete all functions inside a FOR LOOP before iterating to next iteration of the loop

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 15:27:49
问题 Say i have a function like : var bigArray = [1,2,3,4,5.........n]; for(var i=0; i<bigArray.length; i++){ if(someCondition){ setTimeout(function(){ ..do_stuff.. callSomeFunction(); }, someDelayTime); } if(someCondition){ setTimeout(function(){ ..do_stuff.. callSomeFunction(); }, someDelayTime); } if(someCondition){ setTimeout(function(){ ..do_stuff.. callSomeFunction(); }, someDelayTime); } . . . . . . . . if(someCondition){ setTimeout(function(){ ..do_stuff.. callSomeFunction(); },

Node async/await with promise.all

时光怂恿深爱的人放手 提交于 2019-12-10 15:27:47
问题 In a simple JS program, I need to have asyncOperation2 and asyncOperation3 execute sequentially along with asyncOperation1. Meaning I need the following to happen: 1) order of execution as 1,2,3 or 2,3,1 2) then after step 1. I need to preform an operation on the result of 1,2,3 Here's what I have so far(this will run in Node). But I cannot get the order of operations to happen as I described above. const App = { total: 0, init: function () { // I want to run all 3 operations. (with 2 and 3

Express middleware cannot trap errors thrown by async/await, but why?

和自甴很熟 提交于 2019-12-10 14:45:08
问题 These two middleware functions behave differently and I cannot figure out why: Here, the error will get trapped by try/catch: router.get('/force_async_error/0', async function (req, res, next) { try{ await Promise.reject(new Error('my zoom 0')); } catch(err){ next(err); } }); But here, the error will not get trapped by try/catch: router.get('/force_async_error/1', async function (req, res, next) { await Promise.reject(new Error('my zoom 1')); }); I thought Express wrapped all middleware