es6-promise

Unhandled rejection error Bluebird

南笙酒味 提交于 2020-01-13 10:29:21
问题 I have the following code. And it works as expected without throwing a unhandled rejection error. p = new Promise (fulfill, reject) -> reject new Error 'some error' p.catch (error) -> console.log error Now, the second code example does throw an unhandled rejection error. Can someone explain to me why this is happening when im clearly handling the error. p = new Promise (fulfill, reject) -> reject new Error 'some error' p.then -> console.log 'ok' p.catch (error) -> console.log error Btw. I'm

Why is a new promise resolved earlier with p.then(resolve) than with resolve(p)?

最后都变了- 提交于 2020-01-13 06:03:24
问题 The difference between Code#1 and Code#2 is: Code#1 uses resolve(p) and Code#2 uses p.then(()=>resolve()) . I would expect the sequence of output to be invariant, but they generate a different sequence. I cannot figure out why. Code #1: resolve(p) const p = Promise.resolve(); new Promise((resolve) => { resolve(p); // <--- }).then(() => { console.log('after:await'); }); p.then(() => console.log('tick:a')) .then(() => console.log('tick:b')) .then(() => console.log('tick:c')); Output: tick:a

Why is a new promise resolved earlier with p.then(resolve) than with resolve(p)?

人走茶凉 提交于 2020-01-13 06:03:05
问题 The difference between Code#1 and Code#2 is: Code#1 uses resolve(p) and Code#2 uses p.then(()=>resolve()) . I would expect the sequence of output to be invariant, but they generate a different sequence. I cannot figure out why. Code #1: resolve(p) const p = Promise.resolve(); new Promise((resolve) => { resolve(p); // <--- }).then(() => { console.log('after:await'); }); p.then(() => console.log('tick:a')) .then(() => console.log('tick:b')) .then(() => console.log('tick:c')); Output: tick:a

Setting a timeout for each promise within a promise.all

只愿长相守 提交于 2020-01-12 19:11:30
问题 I am able to successfully perform a Promise.all, and gracefully handle resolves and rejects. However, some promises complete within a few milliseconds, some can/could take a while. I want to be able to set a timeout for each promise within the Promise.all, so it can attempt to take a maximum of say 5seconds. getData() { var that = this; var tableUrls = ['http://table-one.com','http://table-two.com']; var spoonUrls = ['http://spoon-one.com','http://spoon-two.com']; var tablePromises = that

Correct pattern for multiway flows with Promises

不想你离开。 提交于 2020-01-12 19:09:29
问题 So i have been playing with promises for the last few days, and just trying to convert some project, to use promises, but more than a few times i have encuntered this issue. While reading articles and tutorials everything looks smooth and clean: getDataFromDB() .then(makeCalculatons) .then(getDataFromDB) .then(serveToClient) But in reality, its not like that. Programs have a lot of "if conditions" that changes the whole flow: getDataFromCache(data).then(function(result){ if(result){ return

Does JavaScript Promise.all have a callback that is fired when there are success AND failures [duplicate]

拈花ヽ惹草 提交于 2020-01-12 06:55:28
问题 This question already has answers here : Wait until all promises complete even if some rejected (17 answers) Closed 3 years ago . Am I misunderstanding Promise.all? I have X promises in an array and i'm trying to aggregate the success/failure ratio of the array. Here is what I think I know: Promise.all takes an array of promises. If all of the promises succeed then the .then callback is ran. If one of the promises fail then the .catch callback is called and the argument passed in is the value

How to return a proper Promise with TypeScript

大兔子大兔子 提交于 2020-01-12 03:57:27
问题 So I am learning Angular 2 with typescript. I am reaching a point to write a mocking service which (I believe) should return a Promise if the service get the Object Successfully and Return an Error if anything happens. I have tried following code but looks like it is not a write syntax for typescript. Updated the CODE: saveMyClass(updatedMyClass: MyClass){ //saving MyClass using http service //return the saved MyClass or error var savedMyClass : MyClass = someLogicThatReturnsTheSavedObject

general solution to retry a promise in javascript

こ雲淡風輕ζ 提交于 2020-01-11 12:30:12
问题 I try to give out a general solution for retrying a promise. Below is my way and comes an error of "Uncaught (in promise)". How can I fix this problem? function tryAtMost(maxRetries, promise) { let tries = maxRetries return new Promise(function(resolve, reject) { promise.then((result) => { resolve(result) }) .catch(err => { if (tries > 0) { console.log(`tries with ${tries}`) tryAtMost(--tries, promise); } else { reject(err) } }) }) } tryAtMost(3, promise).then(result => { console.log(result)

Loop with native promises;

主宰稳场 提交于 2020-01-10 04:57:09
问题 I'm trying to make an asynchronous loop with native ES6 promises It kind of works, but incorrectly. I suppose I made a huge mistake somewhere and I need someone to tell me where it is and how it's done correctly var i = 0; //creates sample resolver function payloadGenerator(){ return function(resolve) { setTimeout(function(){ i++; resolve(); }, 300) } } // creates resolver that fulfills the promise if condition is false, otherwise rejects the promise. // Used only for routing purpose function

How to use promise in forEach loop of array to populate an object

有些话、适合烂在心里 提交于 2020-01-09 06:23:25
问题 I am running a forEach loop on an array and making two calls which return promises, and I want to populate an object say this.options , and then do other stuff with it. Right now I am running into the async issue if i use the following code sample and i get into the then function first. $.when.apply($, someArray.map(function(item) { return $.ajax({...}).then(function(data){...}); })).then(function() { // all ajax calls done now }); This is working code below, but it only works for the first