promise

Synchronous loop in Promise all

我只是一个虾纸丫 提交于 2021-02-18 06:45:14
问题 I would like to do a synchronous loop in a part of my code. The function saveInDatabase checks if the item title (string) already exists in the database. That's why it can't be resolved in parallel, otherwise the condition will never apply (and would create duplicates). Promise.all(arr.map(item => { saveInDatabase(item).then((myResult) => ... ); })); I tried to encapsulate this function into separate promises, also tried with npm packages (synchronous.js, sync), but it seems that it does not

Fetch API always returning a promise

末鹿安然 提交于 2021-02-17 05:54:53
问题 I have the following code where I am calling an api const fetchToken = async () => { const response = await axios.post("http://localhost:3000/api/getRedisData", { key: process.env.AUTH_TOKEN_NAME!, }); console.log(response); // this returns what I expect it to return return response; }; Then I am calling the function like this. fetchToken(); Now, this function works just fine. But when I try to save the data of this response like the following. token = fetchToken().then((r) => r); It returns

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

Request-Promise: do promise cache the result

谁都会走 提交于 2021-02-16 20:05:30
问题 I'm using Request-Promise ( See code below ). Question: If I cache a promise, do it cache the result or each time ask a new one? Example: var cachedPromise = getTokenPromise(); cachedPromise.then(function(authorizationToken1) { //... }); cachedPromise.then(function(authorizationToken2) { //... }); //QUESTION: Is right that authorizationToken1 equals authorizationToken2 getTokenPromise() function: var querystring = require('querystring'); var rp = require('request-promise'); /** * Returns an

Request-Promise: do promise cache the result

五迷三道 提交于 2021-02-16 20:04:30
问题 I'm using Request-Promise ( See code below ). Question: If I cache a promise, do it cache the result or each time ask a new one? Example: var cachedPromise = getTokenPromise(); cachedPromise.then(function(authorizationToken1) { //... }); cachedPromise.then(function(authorizationToken2) { //... }); //QUESTION: Is right that authorizationToken1 equals authorizationToken2 getTokenPromise() function: var querystring = require('querystring'); var rp = require('request-promise'); /** * Returns an

Is it possible to break away from await Promise.all when any promise has fulfilled (Chrome 80)

南楼画角 提交于 2021-02-16 16:11:24
问题 I need to send a request to multi-servers to see which server will response to my request and if any of them response, I will then further interact with this server. The simplest way is to send my request in sequence, something like this async function probing(servers) { for (const server of servers) { const result = await fetch(server) if (result.status == 200) { return result } } } But I hope to speed up the probing process so I change my code to async function probing(servers) { results =

Promise within while loop won't run - causing infinite loop

为君一笑 提交于 2021-02-11 15:56:20
问题 I am trying to run a Promise within a while loop. The code after the promise (in the .then) will eventually break the while loop. However, the promise never runs. The while loop just keeps running infinitely without triggering the promise. Why is this? Is it impossible to use a promise in a while loop? A simplified version of code is below while(relevantRefundRequests.length >= waitList[0].quantity){ stripe.paymentIntents.create({ }) .then(data => { ***code that will break while loop }) } 回答1

Promise within while loop won't run - causing infinite loop

北战南征 提交于 2021-02-11 15:55:53
问题 I am trying to run a Promise within a while loop. The code after the promise (in the .then) will eventually break the while loop. However, the promise never runs. The while loop just keeps running infinitely without triggering the promise. Why is this? Is it impossible to use a promise in a while loop? A simplified version of code is below while(relevantRefundRequests.length >= waitList[0].quantity){ stripe.paymentIntents.create({ }) .then(data => { ***code that will break while loop }) } 回答1