es6-promise

Handling errors in express async middleware

。_饼干妹妹 提交于 2019-11-30 05:47:36
问题 I have an async middleware in express, because I want to use await inside it, to clean up my code. const express = require('express'); const app = express(); app.use(async(req, res, next) => { await authenticate(req); next(); }); app.get('/route', async(req, res) => { const result = await request('http://example.com'); res.end(result); }); app.use((err, req, res, next) => { console.error(err); res .status(500) .end('error'); }) app.listen(8080); The problem is that when it rejects, it doesn't

How does jest.fn() work

时光怂恿深爱的人放手 提交于 2019-11-30 04:47:14
Can anyone explain how does jest.fn() actually work with a real world example , as i'm literally confused on how to use it and where it has to be used. For example if i have the component Countries which fetches country List on click of a button with help of the Utils Function export default class Countries extends React.Component { constructor(props) { super(props) this.state = { countryList:'' } } getList() { //e.preventDefault(); //do an api call here let list = getCountryList(); list.then((response)=>{ this.setState({ countryList:response }) }); } render() { var cListing = "Click button to

promise resolve before inner promise resolved

只谈情不闲聊 提交于 2019-11-30 04:00:21
I have a promise and I want it to resolve only when inner promise has resolved. Right now it resolves before the "resolve" function has been reached in the "loadend" callback. What am I missing? I am confused about the way you are supposed to use resolve and about how you can use a promise within another promise. I couldn't find anything that helped on the web. In the following example I basically load a bunch of files, for each file I get a blob and I want to pass this blob in a file reader. Once all files have been passed to the file reader, I want to move to the next function in the promise

Angular: Return Observable / ES6 Promise from FileReader

*爱你&永不变心* 提交于 2019-11-30 03:12:14
问题 I was trying to return result from FileReader and I found this implementation. But since it is outdated, I'm wondering how to implement the same using ES6 Promises or Rx Observables . Below is my code with reference to the aforementioned link and it works as expected. import { Injectable } from '@angular/core'; import * as XLSX from 'xlsx'; import * as XLS from 'xlsx'; @Injectable() export class ExcelReaderService { constructor() { } importFromExcel(ev): JQueryPromise<any> { let deferred = $

Make server validation using redux-form and Fetch API

混江龙づ霸主 提交于 2019-11-30 02:33:30
How to make server-side validation using redux-form and Fetch API? There are " Submit Validation " demo provided in the docs which says that recommended way to do server side validation is to return a promise from the onSubmit function. But where should I place that promise? As I understood onSubmit function should be my action. <form onSubmit={this.props.addWidget}>... Where this.props.addWidget is actually my action, provided below. import fetch from 'isomorphic-fetch'; ... function fetchAddWidget(widget, workspace) { return dispatch => { dispatch(requestAddWidget(widget, workspace)); return

How to determine if a Promise is supported by the browser

倾然丶 夕夏残阳落幕 提交于 2019-11-29 22:57:58
Does anyone know, using Modernizr or otherwise, if there is a way to detect if the Promise feature is enabled in a browser? I have a polyfill for the functionality, but only want to apply it if the browser does not have a native implementation. Update Dec 11 - 2016: All evergreen versions of browsers now support promises. They are safe to use. Update Nov 14 - 2016: Chrome, Firefox, Safari and IE all now have experimental support for promises in their dev channels. The specification has settled. I would still not rely on the implementation just yet and would use a library but this might change

What are the differences between observables and promises in JavaScript?

强颜欢笑 提交于 2019-11-29 20:48:11
So i've read that observables are looking to overtake promises in terms of usage in some of upcoming JavaScript MVC's: Angular 2.0 Falcor used by Netflix What is the difference between observables and promises? Updated: Apologies! removed my falsy statement. What is the difference between observables and promises? Simply put: A promise resolves to a single value asynchronously, an observable resolves to (or emits) multiple values asynchronously (over time). Concrete examples: Promise: Response from an Ajax call Observable: Click events More information can be found here: http://reactivex.io

How to correctly extract text from a pdf using pdf.js

霸气de小男生 提交于 2019-11-29 19:21:13
问题 I'm new to ES6 and Promise. I'm trying pdf.js to extract texts from all pages of a pdf file into a string array. And when extraction is done, I want to parse the array somehow. Say pdf file(passed via typedarray correctly) has 4 pages and my code is: let str = []; PDFJS.getDocument(typedarray).then(function(pdf) { for(let i = 1; i <= pdf.numPages; i++) { pdf.getPage(i).then(function(page) { page.getTextContent().then(function(textContent) { for(let j = 0; j < textContent.items.length; j++) {

Do I always need catch() at the end even if I use a reject callback in all then-ables?

余生颓废 提交于 2019-11-29 18:37:21
I am putting catches at the end, but they are returning empty object in one particular instance at least. Is a catch necessary for anything unbeknownst, or is it just screwing me up? $( document).ready(function(){ app.callAPI()//a chainable a RSVP wrapper around a jquery call, with its own success() fail() passing forward to the wrapper, so it will either be a resolved or rejected thenable to which is now going to be chained .then( function(env) { //set the property you needed now app.someSpecialEnvObj = env; }, function(rejectMessage){ console.log('call.API() cant set some special env object.

Limited number of javascript promises is not working if running in infinite loop

人盡茶涼 提交于 2019-11-29 17:43:09
My idea is. I have limited number of workers. This workers must do some job. When worker done with job, then he must take other job, until complete all the jobs. I write this code: function createWorker(num){ this.num = num; this.Run = (job) => ( new Promise( (resolve, reject) => { setTimeout(() => { resolve({worker: this, num: this.num, job: job}); }, 5000); } ) ); } function Processing(maxWorkers, jobs){ this.Workers = []; this.Jobs = jobs; this.Go = () => { return new Promise( (resolve, reject) => { while(true){ let worker = this.Workers.pop(); if (!worker) continue; let job = this.Jobs.pop