promise

How to retry a Promise resolution N times, with a delay between the attempts?

☆樱花仙子☆ 提交于 2020-12-06 06:38:17
问题 I want some JavaScript code to take 3 things as parameters: A function returning a Promise. The maximum number of attempts. The delay between each attempt. What I ended up doing is using a for loop. I did not want to use a recursive function : this way, even if there are 50 attempts the call stack isn't 50 lines longer. Here is the typescript version of the code: /** * @async * @function tryNTimes<T> Tries to resolve a {@link Promise<T>} N times, with a delay between each attempt. * @param

Firebase Query Collection And Merge Subcollection Data

天涯浪子 提交于 2020-12-05 07:03:33
问题 I'm trying to figure out a way to get a collection of documents along with their subcollection data. For example, my tree is... -locations -{locationId} -historic -april -airTemp: 12.4 -displayOrder: 4 -august -airTemp: 14.5 -displayOrder: 9 -december -airTemp: 13.5 -displayOrder: 12 etc.. ...where locationId is a document and historic is the subcollection with monthly documents in it. I know how to get the top level collection items and store it into an array but I want to add their

Unexpected unhandledRejection event for promise which rejection does get handled

拟墨画扇 提交于 2020-12-02 08:14:53
问题 Updated , I've now tried explaining the behavior I'm seeing, but it'd still be great to have an answer from a credible source about the unhandledRejection behavor. I've also started a discussion thread on Reddit. Why do I get an unhandledRejection event (for "error f1") in the following code? That's unexpected, because I handle both rejections in the finally section of main . I'm seeing the same behavior in Node (v14.13.1) and Chrome (v86.0.4240.75): window.addEventListener(

Unexpected unhandledRejection event for promise which rejection does get handled

痴心易碎 提交于 2020-12-02 08:12:42
问题 Updated , I've now tried explaining the behavior I'm seeing, but it'd still be great to have an answer from a credible source about the unhandledRejection behavor. I've also started a discussion thread on Reddit. Why do I get an unhandledRejection event (for "error f1") in the following code? That's unexpected, because I handle both rejections in the finally section of main . I'm seeing the same behavior in Node (v14.13.1) and Chrome (v86.0.4240.75): window.addEventListener(

In a Promise, what's the difference between using catch and the 2nd argument of then? [duplicate]

戏子无情 提交于 2020-12-02 06:17:43
问题 This question already has answers here : When is .then(success, fail) considered an antipattern for promises? (7 answers) Closed 4 years ago . What exactly is the difference between these two statements? funcThatReturnsAPromise() .then(() => { /* success */ }) .catch(() => { /* fail */ }); funcThatReturnsAPromise() .then(() => { /* success */ }, () => { /* fail */ }); 回答1: Besides .catch(fn) being a shortcut for .then(null, fn) , the difference in your examples is that funcThatReturnsAPromise

In a Promise, what's the difference between using catch and the 2nd argument of then? [duplicate]

空扰寡人 提交于 2020-12-02 06:11:20
问题 This question already has answers here : When is .then(success, fail) considered an antipattern for promises? (7 answers) Closed 4 years ago . What exactly is the difference between these two statements? funcThatReturnsAPromise() .then(() => { /* success */ }) .catch(() => { /* fail */ }); funcThatReturnsAPromise() .then(() => { /* success */ }, () => { /* fail */ }); 回答1: Besides .catch(fn) being a shortcut for .then(null, fn) , the difference in your examples is that funcThatReturnsAPromise

Promise: Then before Resolve

我的梦境 提交于 2020-11-29 10:40:30
问题 Very first thing I've ever done in Node.js, I'm writing an AWS Lambda function, and I want to check whether a custom attribute on a User has a value before doing anything else. Since I'm told Promises are the way to handle asynchronous methods synchronously, I wrote the function: var AWS = require('aws-sdk'); var s3 = new AWS.S3(); var cogId = new AWS.CognitoIdentityServiceProvider(); exports.handler = function (event, context) { if (event != null) { var identityId = context.identity

Promise: Then before Resolve

╄→尐↘猪︶ㄣ 提交于 2020-11-29 10:40:30
问题 Very first thing I've ever done in Node.js, I'm writing an AWS Lambda function, and I want to check whether a custom attribute on a User has a value before doing anything else. Since I'm told Promises are the way to handle asynchronous methods synchronously, I wrote the function: var AWS = require('aws-sdk'); var s3 = new AWS.S3(); var cogId = new AWS.CognitoIdentityServiceProvider(); exports.handler = function (event, context) { if (event != null) { var identityId = context.identity

Do Javascript promises block the stack

强颜欢笑 提交于 2020-11-24 16:53:15
问题 When using Javascript promises, does the event loop get blocked? My understanding is that using await & async, makes the stack stop until the operation has completed. Does it do this by blocking the stack or does it act similar to a callback and pass of the process to an API of sorts? 回答1: When using Javascript promises, does the event loop get blocked? No. Promises are only an event notification system. They aren't an operation themselves. They simply respond to being resolved or rejected by

what happens when a Promise never resolves? [duplicate]

你说的曾经没有我的故事 提交于 2020-11-24 11:02:34
问题 This question already has answers here : Node exits without error and doesn't await promise (Event callback) (2 answers) Closed 3 years ago . I have this very confusing snippet of code using es6 async await syntax. What I would expect to happen is that the process hangs on the await line forever, since the resolve function is never called. However, what actually happens is that "start" is outputted and then the process exits with no more output. const simple = async () => { console.log('start