es6-promise

Correct handling of async Mongo actions in Node Promise

*爱你&永不变心* 提交于 2019-12-08 15:40:43
I have a function which performs multiple Mongo actions, and the last action is to close the database after all other actions have completed. I was fairly confident of my handling of the issue, but I have had some external comments raise concerns, and I wanted to verify if my solution is correct. A suggested solution: function updateDatabase (name, token) { return new Promise((resolve, reject) => { MongoClient.connect(MONGODB_URL) .then( (database) => { return database.collection('testCollection').update({name}, {$pull: {tokens: {$in: [token]}}}) .then( () => { database.collection('log')

JavaScript promise resolving with setTimeout

余生长醉 提交于 2019-12-08 11:45:58
问题 I don't understand why the first setTimeout function works but the second one doesn't. The first one is commented out when I run the second setTimeout. But instead of resolving after 3 seconds it resolves immediately. I'm new to the whole 'promise' thing and the tutorial I'm working through uses promises with setTimeout a lot. let promise = new Promise( ( resolve, reject ) => { /* why does setTimeout work with this one... */ setTimeout( () => resolve( 'Job\'s done!!!' ), 3000 ); /* but not

Pattern for interrupting heavy computation on browser event

烈酒焚心 提交于 2019-12-08 10:28:15
问题 I'm working on a client side simulation that does on the fly background computation and view refresh. However, because the simulation is always live, the CPU ends up doing a lot of unnecessary work during intense user inputs and edits. What I want to achieve is a way to kill the whole sequence on user event. anticipated usage in the main app: var sequence = new Sequence(heavyFunc1, heavyFunc2, updateDom); document.addEventListener("click", sequence.stop) sequence.run() //all the heavy

stack overflow when returning an ES6 proxy through a promise

断了今生、忘了曾经 提交于 2019-12-08 08:10:48
问题 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

Submit form in React without jQuery AJAX

£可爱£侵袭症+ 提交于 2019-12-08 07:27:40
问题 I'm trying to submit my form using formspree in a static site I'm building with React. I've gotten close, but now completely lost. I'm trying to use ES6 Promise function but don't know how to complete it. Here is my current code: import React from 'react'; import { Link } from 'react-router'; import { prefixLink } from 'gatsby-helpers'; import { config } from 'config'; import Headroom from 'react-headroom'; import Nav from './nav.js'; import '../css/main.scss'; import Modal from 'boron

Correct handling of async Mongo actions in Node Promise

泄露秘密 提交于 2019-12-08 06:20:24
问题 I have a function which performs multiple Mongo actions, and the last action is to close the database after all other actions have completed. I was fairly confident of my handling of the issue, but I have had some external comments raise concerns, and I wanted to verify if my solution is correct. A suggested solution: function updateDatabase (name, token) { return new Promise((resolve, reject) => { MongoClient.connect(MONGODB_URL) .then( (database) => { return database.collection(

Serial execution of functions returning promises

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 05:37:56
问题 With ES2016 we now have promises and that's great. Unfortunately the functionality is very minimalistic and there is nothing like the series or waterfall as available in the async package. If there a package providing this functionality for promises or how do people typically deal with those use cases? 回答1: To serially execute an array of functions returning promises you can use Array.prototype.reduce : let final = functions.reduce((prev, f) => prev.then(f), Promise.resolve()); The "initial"

Handle async promise before ngOnInit Angular

白昼怎懂夜的黑 提交于 2019-12-08 05:08:19
问题 I have a request that returns data for a table, which needs to be handled like a promise to wait until the data has been loaded. In order to load the data into the table I have to use async/wait, but this messes up all other functions/methods. How to store the data in currentList without using async/wait on ngOnInit()? Or is there an other way? async getEducationList(): Promise<any> { return this.educationList = await this.applicationClient.getEducations() .toPromise() .then((res) => { this

Create an array of fetch promises using a for loop with JavaScript/ES6 that can be read via Promise.all?

六月ゝ 毕业季﹏ 提交于 2019-12-08 04:50:40
问题 So, without boring anyone with the backstory, I need to access data from a number of APIs in order to run my script. The data needs to all be loaded before I execute the script, which I'm normally comfortable doing: I just declare some fetch requests, write a Promise.all, then continue on with the function. HOWEVER, I've hit something of a snafu with a certain API that limits the number of results I can pull from one request to 100 and I need to query all of the results. I didn't think this

How does .then(console.log()) and .then(() => console.log()) in a promise chain differ in execution

左心房为你撑大大i 提交于 2019-12-08 02:56:42
问题 Is there any difference in efficiency? Will the behavior be any different if a setTimeout is used instead of console.log() 回答1: You can basically do these three things .then(console.log()) This calls the console.log immediately, without waiting until the promise is resolved, so it is not probably something that you would want to do. .then(console.log) This executes the console.log only after the promise has successfully resolved (requires one function call) and implicitly pass the result of