promise

How to wait a Promise inside a forEach loop

我只是一个虾纸丫 提交于 2021-01-29 00:58:36
问题 I'm using some Promise() functions to fetch some data and I got stuck with this problem on a project. example1 = () => new Promise(function(resolve, reject) { setTimeout(function() { resolve('foo1'); }, 3000); }); example2 = () => new Promise(function(resolve, reject) { setTimeout(function() { resolve('foo2'); }, 3000); }); doStuff = () => { const listExample = ['a','b','c']; let s = ""; listExample.forEach((item,index) => { console.log(item); example1().then(() => { console.log("Fisrt"); s =

How to wait a Promise inside a forEach loop

Deadly 提交于 2021-01-29 00:56:23
问题 I'm using some Promise() functions to fetch some data and I got stuck with this problem on a project. example1 = () => new Promise(function(resolve, reject) { setTimeout(function() { resolve('foo1'); }, 3000); }); example2 = () => new Promise(function(resolve, reject) { setTimeout(function() { resolve('foo2'); }, 3000); }); doStuff = () => { const listExample = ['a','b','c']; let s = ""; listExample.forEach((item,index) => { console.log(item); example1().then(() => { console.log("Fisrt"); s =

Firebase, retrieving data asynchronously

混江龙づ霸主 提交于 2021-01-28 19:29:37
问题 I wrote a promise in javascript to delay an animation until some data is retrieved from my firebase database. The problem is that for some reason the promise isn't working, and the code isn't running asynchronously. Am i not using this promise properly, or is there another issue? var name = document.querySelector('.name') new Promise (function() { firebase.database().ref('users/' + userId ).on('value', function(snap) { console.log('first'); name.innerText = snap.child('name').val(); }); })

How to wait promises ad push data to array in forEach loop [react]

纵然是瞬间 提交于 2021-01-28 18:36:45
问题 So i want to use this effect: useEffect(() => { return db.collection('users').orderBy('lastName').onSnapshot(snap => { const usersArray = [] snap.forEach( async doc => { const user = doc.data() if(user.manager) { await Promise.all([ db.collection('users').doc(user.manager).get(), db.collection('roles').doc(user.profile).get() ]).then(resp => { const manager = resp[0].data() const role = resp[1].data() usersArray.push({ id: doc.id, ...user, manager: `${manager.lastName} ${manager.name} $

How to wait promises ad push data to array in forEach loop [react]

六眼飞鱼酱① 提交于 2021-01-28 18:35:00
问题 So i want to use this effect: useEffect(() => { return db.collection('users').orderBy('lastName').onSnapshot(snap => { const usersArray = [] snap.forEach( async doc => { const user = doc.data() if(user.manager) { await Promise.all([ db.collection('users').doc(user.manager).get(), db.collection('roles').doc(user.profile).get() ]).then(resp => { const manager = resp[0].data() const role = resp[1].data() usersArray.push({ id: doc.id, ...user, manager: `${manager.lastName} ${manager.name} $

How to avoid the explicit Promise construction antipattern for this concurrent timer that should reset when the promise completes?

倖福魔咒の 提交于 2021-01-28 18:20:29
问题 I have written a pretty simply script: { waiting: false, async handleWaiting(promise, timeout) { return new Promise((res, rej) => { let loadingStarted = false; const timeoutInstance = setTimeout(() => { loadingStarted = true; this.waiting = true; }, timeout); const onFinished = () => { if (loadingStarted) { this.waiting = false; } clearTimeout(timeoutInstance); } promise .then((result) => { onFinished(); res(result); }) .catch((ex) => { onFinished(); rej(ex); }); }); }, async searchForTerm

Poll API until a path in the response object is Successful | Failure - Typescript

眉间皱痕 提交于 2021-01-28 11:48:31
问题 I have a function which accepts a string and a path value and checks whether the path at the result returns a 1 or a -1. I fire the function with multiple requests and everything seems to be successful except for one. For example, if I call the function with 10 different URL's continously (one by one, not in an array), the promise is resolved for 9 but not for the 10th one. This is my code: enum Status { Queued = 0, Started = 1, Finished = 2, Failed = -1, } let dataFetchingTimer: number;

Poll API until a path in the response object is Successful | Failure - Typescript

微笑、不失礼 提交于 2021-01-28 11:41:32
问题 I have a function which accepts a string and a path value and checks whether the path at the result returns a 1 or a -1. I fire the function with multiple requests and everything seems to be successful except for one. For example, if I call the function with 10 different URL's continously (one by one, not in an array), the promise is resolved for 9 but not for the 10th one. This is my code: enum Status { Queued = 0, Started = 1, Finished = 2, Failed = -1, } let dataFetchingTimer: number;

Why does this simple JS promise return a promise? [duplicate]

混江龙づ霸主 提交于 2021-01-28 10:06:56
问题 This question already has answers here : How to return value from an asynchronous callback function? [duplicate] (3 answers) Closed 1 year ago . A really simply question. I've boiled it down to the simplest version that illustrates my problem. Why does this vanilla JS return a promise, and how can I get it to return "Hello"? let result = test() .then(function(result) { return result; }); alert(result); function test(serialized) { return new Promise(function(resolve, reject) { resolve("Hello")

Why does this simple JS promise return a promise? [duplicate]

删除回忆录丶 提交于 2021-01-28 09:54:34
问题 This question already has answers here : How to return value from an asynchronous callback function? [duplicate] (3 answers) Closed 1 year ago . A really simply question. I've boiled it down to the simplest version that illustrates my problem. Why does this vanilla JS return a promise, and how can I get it to return "Hello"? let result = test() .then(function(result) { return result; }); alert(result); function test(serialized) { return new Promise(function(resolve, reject) { resolve("Hello")