promise

Execute .Then() promise NodeJS

你。 提交于 2021-01-28 09:41:09
问题 I am trying to figure out how to call a Promise using ".then" so I can continue performing other functions on the return Promise output. I have confirmed the function being used works as expected, but after watching videos and other SO examples am still having a hard time getting this working. See below snippet: const fs = require('fs'); const JSZip = require('jszip'); const directoryFile = fs.readdirSync('./zipped'); //console.log(directoryFile); var zip = new JSZip(); var dir = ('./zipped/'

Javascript async/await execution order problem in for…of, for await…of and Promise.all

邮差的信 提交于 2021-01-28 07:34:59
问题 For each object (product) in an array (products), I'm getting the price from a mongoose database. That value (prodDB.price) is summed to the "amount" variable initialized as 0 before the loop. I tried 3 solutions explained in other questions, with: for of for await of Promise.all --- for of --- let amount = 0; for (const product of products) { await Product.findById(product._id).exec((err, prodDB)=> { amount += product.count * prodDB.price; console.log("Current amount", amount); }); };

Use async function when consumer doesn't expect a promise

拈花ヽ惹草 提交于 2021-01-28 05:29:38
问题 I'm currently struggling to make an interop call work because I can't synchronously wait for a Promise in javascript. Before you think about closing this as duplicate, note that I have done some searching and I understand that it says everywhere it's not possible to synchronously wait for a Promise. Once async, always async. Source(s): How to wait for a JavaScript Promise to resolve before resuming function?, How can I synchronously determine a JavaScript Promise's state?, Call An

void before promise syntax

寵の児 提交于 2021-01-28 04:06:38
问题 What is the actual impact of putting void before promise? async function doAsyncStuff(){ ... } function nonAsyncFunction(){ void doAsyncStuff(); } I couldn't find any official documentation for this, but it must be doing something as it resolves no-floating-promises TSLint error. 回答1: void is an operator that accepts a value on the Right-Hand Side and evaluates as undefined . It resolves no-floating-promises because it does something (or rather, explicitly nothing) with the promise. 来源: https

Axios returns promise pending

只愿长相守 提交于 2021-01-28 04:00:50
问题 i want this function to return either true or false, instead I get /** * Sends request to the backend to check if jwt is valid * @returns {boolean} */ const isAuthenticated = () => { const token = localStorage.getItem('jwt'); if(!token) return false; const config = {headers : {'x-auth-token' : token}}; const response = axios.get('http://localhost:8000/user' , config) .then(res => res.status === 200 ? true : false) .catch(err => false); return response; } export default isAuthenticated; I

Wrapping Promise based JavaScript HTTP client with RxJs Observable

谁都会走 提交于 2021-01-28 01:48:01
问题 I was thinking of using Observables to add .flatMapLatest() & .throttle() functionality to a Promise based HTTP client library (axios). But I'm not going to change the whole application to work with Observables, so I would need something like this: Promise -> Observable -> Promise Anyone managed to do something like this? None of the examples I've found do exactly that. I know that RxJs provides a way to make an Observable from Promise and then convert it back to Promise, but haven't figured

Infinite loop when checking roles. VueJS

ぃ、小莉子 提交于 2021-01-28 01:33:32
问题 There is a navigation bar where elements are displayed depending on the user's role: <b-navbar-toggle right class="jh-navbar-toggler d-lg-none" href="javascript:void(0);" data-toggle="collapse" target="header-tabs" aria-expanded="false" aria-label="Toggle navigation"> <font-awesome-icon icon="bars"/> </b-navbar-toggle> <b-collapse is-nav id="header-tabs"> <b-navbar-nav class="ml-auto"> <b-nav-item-dropdown right id="pass-menu" v-if="hasAnyAuthority('ROLE_USER') && authenticated" :class="{

Axios returns promise pending

旧时模样 提交于 2021-01-28 00:59:33
问题 i want this function to return either true or false, instead I get /** * Sends request to the backend to check if jwt is valid * @returns {boolean} */ const isAuthenticated = () => { const token = localStorage.getItem('jwt'); if(!token) return false; const config = {headers : {'x-auth-token' : token}}; const response = axios.get('http://localhost:8000/user' , config) .then(res => res.status === 200 ? true : false) .catch(err => false); return response; } export default isAuthenticated; I

javascript - how to use promises in firebase?

好久不见. 提交于 2021-01-27 23:19:59
问题 Lately I've been stuck with a problem that I don't know how to solve. I asked this question and after some efforts we've found that Firebase works differently with promises than normal requests, and I couldn't use them properly. As explained in the question, I'm filling an array with some informations from Firebase, and I need to call another method when I'm sure the array is filled, in other words when I'm sure the call to Firebase has finished. This is my code as I'm using it now: var user

Fetching data in React's useEffect() returns “undefined”

 ̄綄美尐妖づ 提交于 2021-01-27 18:38:58
问题 I'm trying to fetch data from a database. It's a get request. All works fine as long I am using the fetched data in the async function. But ouside of it, it just returns "undefined". What am I doing wrong? Thanks for your help :) const [accountInfos, setAccountInfos] = useState(); const [error, setError] = useState(); const [loading, setLoading] = useState(true); useEffect(() => { (async function runEffect() { const fetchedAccountInfos = await fetchAccountInfos(); if (fetchedAccountInfos &&