es6-promise

Observable - converting 2 promises into an observable

徘徊边缘 提交于 2019-12-12 02:39:17
问题 I have a popular scenario where I need to create one promise that returns data which is fed to a second promise. If the first promise fails, I need to cancel the second promise. In 'Promise' land it would look something like this: Fn1.doPromise( initialData ) .then(info => { Fn2.doPromise( info ) .then(result => { //success - return result }) .catch(error => { //error }); }) .catch(error => { //cancel 2nd promise and show error }); Now I am trying to learn the best way to do this using

How to save multiple mongodb collections using Promise?

亡梦爱人 提交于 2019-12-12 02:38:55
问题 using async library I can save data using below code function save(callback){ async.parallel([ function(cb){ Collection1.collection.insert(collections1,cb); }, function(cb){ Collection2.collection.insert(collections2,cb); }, function(cb){ Collection3.collection.insert(collections3,cb); } ],callback); } save(function(){ console.log(arguments); }); How I can modifiy save function so it will return promise ? save() .then(function(){ console.log('success'); }) .catch(function(){ console.log(

ES6 giving SyntaxError: Unexpected token ) [duplicate]

风格不统一 提交于 2019-12-12 01:37:40
问题 This question already has answers here : ECMAScript 6 features available in Node.js 0.12 (2 answers) Closed 3 years ago . Here is my Node.js script: pDownload("http://serv/file", "localfile") .then( ()=> console.log('downloaded file no issues...')) .catch( e => console.error('error while downloading', e)); function pDownload(url, dest){} // Yes empty, for now When executing on Node.js v0.10.25 (default on Ubuntu 2015.10) I receive this syntax error about the parenthesis: /home/nico/main.js:2

Why do I need to use async/await twice in node-postgres

走远了吗. 提交于 2019-12-11 19:06:39
问题 I wrote this code that seems to be working: database.js const {Pool} = require('pg'); const pool = new Pool({ connectionString: process.env.DATABASE_URL, }); module.exports = { query: (text, params) => pool.query(text, params) }; auth_facade.js const database = require('../../utils/database'); module.exports.findPersonByEmail = async function(email) { const query = 'SELECT * FROM Person WHERE email = $1'; const values = [email]; try { console.log(1); const {rows} = await database.query(query,

JS promise is getting called without calling it

我的梦境 提交于 2019-12-11 17:36:56
问题 I don't know why is promise1 keeps getting called even though I never tried to resolve it. function successCallback() { console.log("doSomething func succeeded with sucess"); } function failureCallback() { console.log("doSomething func failed with error"); } let promis1 = new Promise((res, rej) => { setTimeout(() => { console.log(`Finally got called`); return res(successCallback()); }, 5000); }); function promise2(value) { return new Promise((res, rej) => { console.log(`This is getting called

React JS & Axios chaining promies

你离开我真会死。 提交于 2019-12-11 16:24:24
问题 I am developing a react js application and we are using a promise based library axios for calling APIs. Now, in the initial part of application, user gets a login page, when the login is successful, we contact different systems to retrieve some extra information about user. axios .get('url to authentication endpoint') // 1st call .then(response => { // if login is successful // 1. retrieve the user preferences like, on the customised screens what fields user wanted to see axios.get('user

I'm getting a 'Wrapped promise not iterable error' even though I have only one parameter

别等时光非礼了梦想. 提交于 2019-12-11 15:28:34
问题 I'm trying to use a Promise.all in my node.js microservice. The intent of the Promise.all is to go through all the elements in an array (of queries), and via apolloFetch, calls another microservice, which then executes those queries in a database and then returns back success or error. I'm getting a 'Wrapped promise is not iterable' error - I checked a few posts on SO that have similar errors but in all those cases there are 2 arguments being passed, whereas I'm passing just one - EXCEPT that

How to use js async/await in mutiple async requests

假如想象 提交于 2019-12-11 14:45:49
问题 I'm using Angular 7, now there is a method(Angular guard CanActivate) that contains some nested http call methods, i need to return data after all nested http call finished. As below code shows, only after getCurrentUser() finished, then return result in canActivate() , while now, it always return false because getCurrentUser() haven't finished. export class AuthGuard implements CanActivate{ constructor(private commonService: CommonService) { } async canActivate(next: ActivatedRouteSnapshot,

React : How to display Error if no record match

坚强是说给别人听的谎言 提交于 2019-12-11 14:16:35
问题 I am working on React Project . Actually , I implemented search filter but I have some problem regarding search filter . I want to show message if there is no match record found but currently when I type something if record found it showed to the user but when I type something which are not matched it not show error message instead it showing existing record . I want to set error message if record found then show record if record is not found show error message instead of showing existing all

Fetch call freezes

雨燕双飞 提交于 2019-12-11 14:10:25
问题 I am using fetch to read data from a API: async _getDcnDetail(dcnId) { return await fetch(API_URL+'get_dcndetail', { method: "POST", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization':'Bearer '+ this.state.accessToken }, body: JSON.stringify({ DcnId: dcnId }) }).then(response => response.json()); } Then I call it: async componentDidMount() { let response = await this._getDcnDetail(dcn.DcnId); console.log(response); } But it "waits" eternally, it is