es6-promise

Mocha exceeding 2000ms timeout when returning a promise [duplicate]

北城以北 提交于 2019-11-29 17:33:24
This question already has an answer here: Change default timeout for mocha 4 answers I'm trying to test my Express.js controllers but every so often I run into a problem with the mocha timeout telling me off. The docs ( https://mochajs.org/#working-with-promises ) and the answer here: https://stackoverflow.com/a/26572442/1646372 state that I can just return the promise I'm using. I've wrapped my express controllers with Promises so that I can then return them in the tests. I have a basic test that I can run to consistently get the error message: it('should return', () => { return new Promise

Async await strange behaviour with functions

我与影子孤独终老i 提交于 2019-11-29 17:13:43
I have following asynchronous code example: // Functions function getSomePromise() { let a = new Promise((resolve, reject) => { setTimeout(function(){ console.log("Inside promise..."); resolve("Success!"); }, 1000); }); return a; } async function someWrapper(i) { console.log('A: '+ i); await getSomePromise(); console.log('B: ' + i); } And two tests: async function test1() { for(let i=0; i<5; i++) { // body copy-pasted of someWrapper function: console.log('A: '+ i); await getSomePromise(); console.log('B: ' + i); } } async function test2() { for(let i=0; i<5; i++) { someWrapper(i); } } And here

how to properly throw an error if promise is rejected? (UnhandledPromiseRejectionWarning)

别等时光非礼了梦想. 提交于 2019-11-29 15:46:17
I have a promise and I would like an exception to be thrown if the promise is rejected. I tried this: var p = new Promise( (resolve, reject) => { reject ("Error!"); } ); p.then(value => {console.log(value);}); but I get a DeprecationWarning: (node:44056) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error! (node:44056) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. What is the correct way to throw an error (so that the program

Async / await assignment to object keys: is it concurrent?

泪湿孤枕 提交于 2019-11-29 14:06:17
I know that doing this: const resultA = await a() const resultB = await b() // code here Is effectively a().then( resultA => { b().then( resultB => { // code here }) }) Basically, a() runs then b() runs. I nested them to show that both resultA and resultB are in our scope; yet both function didn't run at once. But what about this: const obj = { result1: await a(), result2: await b() } do a() and b() run concurrently? For reference: const asyncFunc = async (func) => await func.call() const results = [funcA,funcB].map( asyncFunc ) I know here funcA and funcB do run concurrently. Bonus : How

Loop with native promises;

自古美人都是妖i 提交于 2019-11-29 13:55:51
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 controller(condition){ return function(resolve, reject) { console.log('i =', i); condition ? reject(

How do I handle multiple browser scripts making the same calls to the back-end service

久未见 提交于 2019-11-29 12:08:05
I have a web page where different parts of it all need the same back-end data. Each is isolated, so they each end up eventually making the same calls to the back-end. What is the best way to avoid making a call to the web server when one is already in progress and initiated by a different piece of code on the same web page? Here's an example. I'll use setTimeout to simulate an asynchronous call. Let's assume there's an async function that returns the list of contacts, which is basically a simple array of strings in this example: var getContacts = function() { log('Calling back-end to get

Use a promise in Angular HttpClient Interceptor

血红的双手。 提交于 2019-11-29 11:20:08
问题 Can I use promise within HttpInterceptor ? For example: export class AuthInterceptor implements HttpInterceptor{ this.someService.someFunction() .then((data)=>{ //do something with data and then return next.handle(req); }); } why I need this? because I need to get a token to add to request header before making the request to the server. My interceptor: @Injectable() export class AuthInterceptor implements HttpInterceptor{ constructor(private authService: AuthService){} intercept(req:

chaining async method calls - javascript

最后都变了- 提交于 2019-11-29 09:21:10
You have a prototype object Foo with two async method calls, bar and baz. var bob = new Foo() Foo.prototype.bar = function land(callback) { setTimeout(function() { callback() console.log('bar'); }, 3000); }; Foo.prototype.baz = function land(callback) { setTimeout(function() { callback() console.log('baz'); }, 3000); }; We want to do bob.bar().baz() and have it log "bar" and "baz" sequentially. If you cannot modify the method calls (including passing in your callback function), how can you pass a default callback into these method calls? Some ideas: Wrap "bob" with decorator (still fuzzy on

Why does JavaScript's `Promise.all` not run all promises in failure conditions?

扶醉桌前 提交于 2019-11-29 07:22:30
According to MDN : If any of the passed in promises rejects, the all Promise immediately rejects with the value of the promise that rejected, discarding all the other promises whether or not they have resolved. The ES6 spec seems to confirm this. My question is: Why does Promise.all discard promises if any of them reject, since I would expect it to wait for "all" promises to settle , and what exactly does "discard" mean? (It's hard to tell what "discard" means for in-flight promises vs. promises that may not have run yet.) I ask because I frequently run into situations where I have a list of

JS ES6 Promise Chaining

为君一笑 提交于 2019-11-29 06:56:25
I'm trying to learn how to use promises, but am having trouble comprehending the chaining. I assume that with this code, both promises will run. Then when I call test.then() it should know that test has resolved and pass the resolve data to then(). Once that function finishes, it goes onto the next then(), repeating the same process with the test2 promise. However, I can only get it to print out the first promise results, not the second. Any ideas what is missing here? var test = new Promise(function(resolve, reject){ resolve('done1'); }); var test2 = new Promise(function(resolve, reject){