es6-promise

Node.js UnhandledPromiseRejectionWarning even after catching it

风格不统一 提交于 2019-12-07 03:18:28
问题 I'm using Node 7.2.1 with the new async/await feature. I'm also using the Native ES6 Promises with mongoose like this - const mongoose = require('mongoose'); mongoose.Promise = global.Promise; My code flow is like this - async function getFollowers(){ try { const followers = await User.getFollowersFromMongo(req.params.userId); res.send(followers); } catch (err) { winston.error('Printing Error = ', err); res.status(400).send({success: false, error: err}); } } UserSchema.statics

es6 promises swallow type errors

穿精又带淫゛_ 提交于 2019-12-07 03:11:11
问题 I want the browser to show an error message when a type error occurs. errors like can not read property something of undefined or undefined reference . new Promise(function(resolve,reject){ // do stuff ... reject('something logical is wrong'); }).catch(e => console.error(e)); new Promise(function(resolve,reject){ // do stuff, and a syntax error :/ var a = { }; a.something.otherthing = 1; /* we have an error here */ // ... }).catch(e => console.error(e)); In the first example the error is a

Whats the purpose of typescript's __awaiter

早过忘川 提交于 2019-12-07 01:21:31
问题 consider this simple class class Test { private foo(): Promise<void> { return new Promise<void>((resolve, reject) => { resolve(); }); } private async bar() { await this.foo(); } } This get compiled into var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator

Should synchronous code called by Promise .then create a new Promise

拟墨画扇 提交于 2019-12-06 23:36:07
问题 I've implemented some code, where asynchronous code is followed by some synchronous functions. For example: function processSomeAsyncData() { asyncFuncCall() .then(syncFunction) .catch(error); } If I understand correctly then is also a Promise. Then, should I create a promise in the synchronous code as well? function syncFunction() { const p = new Promise (function (resolve, reject) { //Do some sync stuff ... resolve(data); } return p; } If that isn't necessary, how do you reject the promise

How to get custom server-side error message with ecmascript-6 and fetch api?

一曲冷凌霜 提交于 2019-12-06 16:49:31
问题 When a client-side fetch request results in an error on the server side, I'd like to return an error code (400) and a custom message. I don't know how to retrieve both on the client-side elegantly using fetch and promises. return fetch('/api/something') .then(response => response.json()) .then(json => { console.log(json.message) // I only have access to the json here. // I'd also like to get the response status code // (from the response object) and potentially // throw an error complete with

stack overflow when returning an ES6 proxy through a promise

蓝咒 提交于 2019-12-06 15:55:55
I'm trying to intercept a method call on a ES6 proxy to be able to do stuff in between with the information I get from the proxy. Now in my case there is quite some stuff going on before creating and returning the proxy from some kind of factory. Because of all this stuff I decided to wrap the prerequisites into a promise-function so that I can chain the proxy creation right onto it and return the resulting proxy through the promise chain. Here's the code to reproduce the problem: proxy_factory.min.js 'use strict'; // require('harmony-reflect'); class ProxyFactory { create(options) { const

How to support API calls to use a Promise or Callbacks with Standard Promises, no defers or external Promise Library?

北战南征 提交于 2019-12-06 15:29:33
问题 I'm wondering how to support Promise 's and Callbacks in an API. So far I've read through a few articles to try to implement however all these articles use deferred or a third party Promise library that has Deferred's. I want to use Native ES Promises. I wanted to know how to implement something like this. Please do not reference Promisify or any third party library as I actually want to implement this. The function signature is like the following: function (callback) { if (callback) { //

What will happen when return new Promise((resolve, reject) => {}) forgot to call either resolve or reject? [duplicate]

故事扮演 提交于 2019-12-06 14:29:19
问题 This question already has answers here : Are JavaScript forever-pending promises bad? (2 answers) Closed 7 months ago . The problem is like this function demo() { return new Promise((resolve, reject) => { ... // The problem here!! //I just found in some rare case we failed to call resolve or reject }) } demo() .then(res => { console.log('resolve') console.log(res) }) .catch(rej => { console.log('reject') console.log(rej) }) .finally(() => { console.log('why') }) When I failed to call resolve

how do I perform a large batch of promises? [duplicate]

喜夏-厌秋 提交于 2019-12-06 13:28:08
This question already has answers here : ES6 Promise replacement of async.eachLimit / async.mapLimit (3 answers) Closed 11 months ago . I am planning on running a large number of queries on firebase which could grow to the order of a few hundred thousand to even millions. I've been using Promise.all() to resolve most of my queries but as the requests grow Promise.all() seems to just stop running at a random number.Ive looked into using Promise.map() but Im not sure if the concurrency will solve the problem. Thank you for your help. Below is a simplified example as you can see it appears to

Unit testing logic inside promise callback

£可爱£侵袭症+ 提交于 2019-12-06 13:09:13
I have an ES6 / Aurelia app that I am using jasmine to test. The method I am trying to test looks something like this: update() { let vm = this; vm.getData() .then((response) => { vm.processData(response); }); } Where this.getData is a function that returns a promise. My spec file looks something like this: describe('my service update function', () => { it('it will call the other functions', () => { myService = new MyService(); spyOn(myService, 'getData').and.callFake(function() { return new Promise((resolve) => { resolve(); }); }); spyOn(myService, 'processData').and.callFake(function() {