es6-promise

Why do promises execute at the point of declaration?

我是研究僧i 提交于 2021-02-19 03:11:30
问题 I would like to execute a set of promises using Promise.all(). My approach is to put these promises in an array and then pass the array to Promise.all(). However, I find that the promises start executing as soon as they are declared and do not even wait for Promise.all to be called. Why is this happening and how can I have the promises only execute upon calling Promise.all()? let promiseArray = []; const values = [1, 2, 3, 4, 5]; values.forEach((value)=>{ promiseArray.push( new Promise(

Why do promises execute at the point of declaration?

放肆的年华 提交于 2021-02-19 03:11:15
问题 I would like to execute a set of promises using Promise.all(). My approach is to put these promises in an array and then pass the array to Promise.all(). However, I find that the promises start executing as soon as they are declared and do not even wait for Promise.all to be called. Why is this happening and how can I have the promises only execute upon calling Promise.all()? let promiseArray = []; const values = [1, 2, 3, 4, 5]; values.forEach((value)=>{ promiseArray.push( new Promise(

How does Promise run when .then method is not called?

巧了我就是萌 提交于 2021-02-18 20:51:10
问题 I create two promises, but I do not run the then method on those promises. Yet once the promise objects go out of scope, the promise code runs as if .then was called. How is a Promise settled without a call to the .then method? I am asking because I would like to load an array with Promise objects and then run the promises in sequence. function promises_createThenRun() { const p1 = createPromise1(); const p2 = createPromise2(); console.log('before hello'); alert('hello'); console.log('after

What is the meaning of the `async` keyword?

安稳与你 提交于 2021-02-17 07:18:34
问题 I have been reading up on async/await in node.js. I have learnt that the await keyword waits for a promise to be resolved, or throws an exception if it was rejected. I have also learnt that every function that wants to use await needs to be marked async . However, what does it mean for a function to be marked async ? All the resources and blog posts I was able to find seem to explain await in great detail, but ignore the concept of an async function, or briefly gloss over it. For instance,

Receiving Promise <Pending> instead of value

邮差的信 提交于 2021-02-17 02:02:13
问题 I have a a object that when I'm printing that it's returning Promise <Pending> (I've checked the type of getRateable and it is object) getRateable = getRateableEntitiesTx(tx, hashtagList); I can't have access to the value by this : getRateableEntitiesTx(tx, hashtagList).then((res) => {return res}) If it's a Promise why it's not returning the res properly? Thanks in advance for help 回答1: You can't return the value from an async function because the function returns before the value has been

Promisifying API callbacks - How to properly resolve or reject

不打扰是莪最后的温柔 提交于 2021-02-16 21:00:35
问题 I've read similar posts, but none quite hit on the head how to do this correctly. I understand Promises and how they are typically created with success and failure listeners waiting to be triggered to either resolve or reject. What I don't understand is when I'm calling an API method that takes a success and failure callback as parameters - how do I determine which callback is being triggered so I can then have it resolved or rejected? For example with this Web API and considering the

Promisifying API callbacks - How to properly resolve or reject

大憨熊 提交于 2021-02-16 21:00:10
问题 I've read similar posts, but none quite hit on the head how to do this correctly. I understand Promises and how they are typically created with success and failure listeners waiting to be triggered to either resolve or reject. What I don't understand is when I'm calling an API method that takes a success and failure callback as parameters - how do I determine which callback is being triggered so I can then have it resolved or rejected? For example with this Web API and considering the

Call a Promise from an Angular HttpInterceptor

旧时模样 提交于 2021-02-16 20:07:38
问题 I've got an angular HttpInterceptor and I need to call an encryption method that's defined like so: private async encrypt(obj: any): Promise<string> { I'm not sure how to handle this in the HttpInterceptor though: intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const modified = req.clone({ body: this.encrypt(req.body) }); return next.handle(modified).pipe( I'm not sure how to tie the two of those together so that I can call the encrypt method properly from

Call a Promise from an Angular HttpInterceptor

£可爱£侵袭症+ 提交于 2021-02-16 20:06:32
问题 I've got an angular HttpInterceptor and I need to call an encryption method that's defined like so: private async encrypt(obj: any): Promise<string> { I'm not sure how to handle this in the HttpInterceptor though: intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const modified = req.clone({ body: this.encrypt(req.body) }); return next.handle(modified).pipe( I'm not sure how to tie the two of those together so that I can call the encrypt method properly from

Call a Promise from an Angular HttpInterceptor

此生再无相见时 提交于 2021-02-16 20:06:20
问题 I've got an angular HttpInterceptor and I need to call an encryption method that's defined like so: private async encrypt(obj: any): Promise<string> { I'm not sure how to handle this in the HttpInterceptor though: intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const modified = req.clone({ body: this.encrypt(req.body) }); return next.handle(modified).pipe( I'm not sure how to tie the two of those together so that I can call the encrypt method properly from