promise

Multiple queries in a .map function

孤者浪人 提交于 2021-01-07 01:36:16
问题 const returnPostData = async (req, res, initialPostsQueryArray) => { try{ const promises = initialPostsQueryArray.map( async (post) => { let voteCount = 0; let voted = false; let liked = false; let userHandle; let userImageUrl; let votingOptionsDictionary; let userVoteOption; voteCount = sumValues(post.voting_options); pool.query('SELECT userhandle, image_url FROM users WHERE id = $1 ', [post.user_id], (error, results) => { if (error) { console.log(error); return res.json({'Error': error

Multiple queries in a .map function

大憨熊 提交于 2021-01-07 01:33:15
问题 const returnPostData = async (req, res, initialPostsQueryArray) => { try{ const promises = initialPostsQueryArray.map( async (post) => { let voteCount = 0; let voted = false; let liked = false; let userHandle; let userImageUrl; let votingOptionsDictionary; let userVoteOption; voteCount = sumValues(post.voting_options); pool.query('SELECT userhandle, image_url FROM users WHERE id = $1 ', [post.user_id], (error, results) => { if (error) { console.log(error); return res.json({'Error': error

Store Promise in Map to resolve/reject later

蓝咒 提交于 2021-01-07 01:24:33
问题 I'm working on IPC in NodeJS and want to be able to send a message to the parent process from the child and "wait" for the result. My idea was to keep track of all the send messages in a map that maps the unique message ID to a promise. Once the process.on('message`) has been called I lookup the promise by the ID I got back from the parent and want to resolve or reject the promise. I came up with this, but am stuck at the resolve/reject part: 'use strict' import RequestMessage from "..

Store Promise in Map to resolve/reject later

孤街醉人 提交于 2021-01-07 01:21:16
问题 I'm working on IPC in NodeJS and want to be able to send a message to the parent process from the child and "wait" for the result. My idea was to keep track of all the send messages in a map that maps the unique message ID to a promise. Once the process.on('message`) has been called I lookup the promise by the ID I got back from the parent and want to resolve or reject the promise. I came up with this, but am stuck at the resolve/reject part: 'use strict' import RequestMessage from "..

Native JS Promise Retry Wrapper

别说谁变了你拦得住时间么 提交于 2021-01-05 09:47:10
问题 Problem Overview: I have a GreaseMonkey/TamperMonkey script in FireFox that sends several GM_xmlhttpRequest ( essentially similar to a xml http request, however this wrapper allows ease in cross-origin requests ) to URL's to retrieve simple JSON data. Some of the JSON data retrieved is super simple, like so, {id:123459876, code:"frog"} [{value:"water", contents:0}] This leads me to my outline of what code I've got down for my programming task so far, and, eventually shapes what I'll ask in

Native JS Promise Retry Wrapper

六眼飞鱼酱① 提交于 2021-01-05 09:46:54
问题 Problem Overview: I have a GreaseMonkey/TamperMonkey script in FireFox that sends several GM_xmlhttpRequest ( essentially similar to a xml http request, however this wrapper allows ease in cross-origin requests ) to URL's to retrieve simple JSON data. Some of the JSON data retrieved is super simple, like so, {id:123459876, code:"frog"} [{value:"water", contents:0}] This leads me to my outline of what code I've got down for my programming task so far, and, eventually shapes what I'll ask in

Add timer to Promise.all, map

蓝咒 提交于 2021-01-05 07:25:25
问题 I would like to add some timeout between below get requests. I mean, flow should be looks like: timeout, https://example.com/example1, timeout, https://example.com/example2, timeout, https://example.com/example3, timeout etc. (or without first timeout, whatever). Below function is working properly: function promiseGetInformationFromMultipleUrls(parts) { return Promise.all(parts.map(part => { return request({ 'url': `https://example.com/${part}`, 'json': true }).then(partData => {console.log(

Firebase (Angular) Promises with Loops in Loops

别等时光非礼了梦想. 提交于 2021-01-04 05:31:40
问题 How do you execute a function only after all the promises are resolved where you have to wait for async calls in loops within loops? The code is simplified to the minimum $scope.book = function(input) { //Get the cart items from DB ref.child('/cart/' + userId).once('value').then(function(cartSnap) { //Loop through the cart items cartSnap.forEach(function(cartItemSnap) { var itemId = cartItemSnap.key(); //Get the inventory items from DB that are in the cart ref.child('/inventory/' + itemId)

TypeScript: Promise.all doesn't handle union types

泪湿孤枕 提交于 2021-01-02 06:32:49
问题 I have the following piece of code that results in compile error in TypeScript: type Promisable = (() => Promise<string>) | (() => Promise<number>); const func = async (promisable: Promisable) => { await Promise.all([promisable()]); }; The error is as follows No overload matches this call. The last overload gave the following error. Argument of type '(Promise | Promise)[]' is not assignable to parameter of type 'Iterable>'. The types returned by 'Symbol.iterator.next(...)' are incompatible

Is there any way to resolve a promise synchronously? (or an alternative library that can)

折月煮酒 提交于 2021-01-01 04:56:26
问题 I have a method for validating a string, I want that method to return a Promise as the validations being ran may be asynchronous. The issue I am having however is one of performance, I want the promise to resolve in the same event loop when possible (eg: when there are no asynchronous validations to be done) but I want the interface to remain consistent (eg: to always return a Promise). The simplified code example below illustrates what I'm trying to do, but it incurs the aforementioned