es6-promise

How to wait for multiple asynchronous calls from for loop?

落花浮王杯 提交于 2019-12-24 11:03:37
问题 Code without any handling: for (i=0; i<dbImgCount; i++){ (function(i) { imgDownload(FolderPath[i].FolderPath, function (next){ var url = FolderPath[i].FolderPath; const img2 = cv.imread(imgDownload.Filename); match({url, img1, img2, detector: new cv.ORBDetector(), matchFunc: cv.matchBruteForceHamming, }); }) })(i); } In the above code, imgDownload is an async function which will download image, match will match features of downloaded image with another image. Need to execute a function after

How can I use promises to catch errors when they might not be wrapped in a promise?

試著忘記壹切 提交于 2019-12-24 10:57:40
问题 Background I am using Promises, and I have a multitude of functions that may or may not return a Promise and that may or may not fail, like in the example below: //does not return a Promise, simply a string let goodFun = function(){ return "I like bananas!"; }; //blows up! let badFun = function(){ throw "A general error blaahh!"; }; //You get the point ... Since these functions may or may not return Promises and may or may not fail, I need to wait for all of them to execute. To achieve this I

How to pass parameters in an array of functions using a series of promises

丶灬走出姿态 提交于 2019-12-24 09:58:03
问题 I have the following functions of Promises: const func1 = () => new Promise((resolve, reject) => { console.log('func1 start'); setTimeout(() => { console.log('func1 complete'); resolve('Hello'); }, 1000); }); const func2 = () => new Promise((resolve, reject) => { console.log('func2 start'); setTimeout(() => { console.log('func2 complete'); resolve('World'); }, 2000); }); And to execute those functions in series, I use: const promiseSerial = funcs => funcs.reduce((promise, func) => promise

Javascript Promise return value

筅森魡賤 提交于 2019-12-24 08:56:30
问题 I am trying to make a crawler, and as the data is not showing in the page source, I can only execute the javascript with the web driver and get the response, and then do the data analysis. The script is simplified, like this, use Promise. var res = "" function f1() { p = window.Promise a = p.resolve(5).then(function(value) { console.log(value) res = value return res }) return a } console.log(f1()) // Promise object console.log("result = ", res) // res is empty My program is like this, writing

Why does returning snapshot.val() in a Promise when using Promise.all not work?

三世轮回 提交于 2019-12-24 08:47:40
问题 I'm writing a Firebase Cloud Function and I'm trying to figure out how Promise.all works. In my code, I pass in an array of database queries and I'm trying the read the resulting array, but I'm only getting garbage: T { A: P { k: Sb { Ka: [Function: vb], ba: [Object] }, aa: P { k: [Object], aa: null, wb: [Object], Bb: '' }, wb: Zc { ld: [Object], ac: [Object] }, Bb: null }, V: R { u: Gd { app: [Object], L: [Object], Ua: [Object], Sc: null, ca: [Object], td: 1, Qa: [Object], va: [Object], qg:

async API call inside forEach loop

流过昼夜 提交于 2019-12-24 08:19:03
问题 I ran into a problem that I cannot seem to solve, I'm guessing I'm missing some points in terms of async behaviour. The task is relatively simple: A wallet with money in different currencies needs to be transformed into a single currency. I want to get the exchange rate from following API: https://free.currencyconverterapi.com/api/v5/convert?q=${from}_${to}&compact=y With ${from} being the starting currency and ${to} the target currency. I store the different currencies in an object called

Nested Firebase Firestore forEach promise queries

╄→гoц情女王★ 提交于 2019-12-24 00:36:53
问题 I am using Firebase Cloud Firestore, however, I think this may be more of a JavaScript promise issue. I have a collection called "students" which I am querying. For each found student I want to issue another query to find "parents" related by id. For this, I have to nest a promise / foreach query and result inside another promise / foreach query and result. Its currently executing the entire "students" promise/loop, returning from the function, then executing each of the "parents" promise

Return value from an async function

﹥>﹥吖頭↗ 提交于 2019-12-24 00:09:20
问题 How can I return the value from an async function? I've the following Promise: function reqGitActivity (url) { const options = { url: url, headers: { 'User-Agent': 'request' } } return new Promise((resolve, reject) => { request(options, (err, res, body) => { if (err) { reject(err) return } resolve(body) }) }) } Then I use this Promise with Async/Await async function githubActivity () { const gh = await reqGitActivity(`https://api.github.com/users/${github}/events`) return gh } And if I

completing a promise without reject/resolve?

一笑奈何 提交于 2019-12-23 20:18:51
问题 I got a custom confirm dialog which waits for user input. I'm wrapping it in a promise. When the user is choosing the "yes" alternative I resolve the promise. However, when the user chooses no it's not really an error but more that the next task should not be executed. How should I handle that scenario with the promise? simply not invoke resolve/reject or is there a better approach? 回答1: You could resolve a value and check that value afterwards. new Promise((resolve, reject) => { const

How to run same promises one after another NodeJs

此生再无相见时 提交于 2019-12-23 19:27:52
问题 I am trying to solve the following problem. Consider the following case. I need to check if an array of servers is alive. Or to be more specific I need to find the first working server from the provided list, I need to do this one by one. For example if the first server doesn't work, check another and another ... As far as NodeJS is asynchronous I cannot do this in a for loop. So I tried to implement something similar to recursion, it looks ugly and doesn't work, but I've tried) static