es6-promise

Javascript: SyntaxError: await is only valid in async function

吃可爱长大的小学妹 提交于 2019-12-12 12:17:43
问题 I am on Node 8 with Sequelize.js Gtting the following error when trying to use await . SyntaxError: await is only valid in async function Code: async function addEvent(req, callback) { var db = req.app.get('db'); var event = req.body.event db.App.findOne({ where: { owner_id: req.user_id, } }).then((app) => { let promise = new Promise((resolve, reject) => { setTimeout(() => resolve("done!"), 6000) }) // I get an error at this point let result = await promise; // let result = await promise; //

What are the differences between jQuery.when() and ES6's Promise.all()?

我的未来我决定 提交于 2019-12-12 11:24:48
问题 I learned promises with jQuery and then didn't do much programming for a couple of years. Now I want to do some stuff using native ES6 promises. Promises bent my head a little back then. Now with both being quite rusty on top of that and there being minor and major differences between jQuery promises, other promise libraries, and the new native JS promises, my head gets even more bent when I try to get this stuff working. It seems like jQuery.when() and Promise.all() do the same thing, but

Extend Javascript promise and resolve or reject it inside constructor

徘徊边缘 提交于 2019-12-12 10:38:54
问题 I want to extend native Javascript Promise class with ES6 syntax, and be able to call some asynchronous function inside the subclass constructor. Based on async function result the promise must be either rejected or resolved. However, two strange things happen when then function is called: subclass constructor is executed twice "Uncaught TypeError: Promise resolve or reject function is not callable" error is thrown class MyPromise extends Promise { constructor(name) { super((resolve, reject)

Why does JS promise print all the resolve first then rejects second

混江龙づ霸主 提交于 2019-12-12 08:52:53
问题 Why does promise print all the success first then the rejects after, even though i wrote the code for it to appear randomly var errors = 0; var successes = 0; var p1; for (var i = 0; i < 10; i++) { p1 = new Promise(function(resolve, reject) { var num = Math.random(); if (num < .5) { resolve(num); } else { reject(num) } }); p1.then(function success(res) { successes++; console.log("*Success:* " + res) }).catch(function error(error) { errors++ console.log("*Error:* " + error) }); } OUTPUT VM331

Catching All Promise Rejections in an Async Function in JavaScript

假如想象 提交于 2019-12-12 07:59:17
问题 I've ran into an issue with catching all the errors when multiple promises throw rejection errors after being awaited in an async function (javaScript - node v8.4.0). Make reference to the following javaScript: For reference, the functions timeoutOne() and timeoutTwo() simply return a native promise that resolves a value after a 1 and 2 second timeout respectively, or reject with an error if I set "deviousState" to true. let deviousState = true; async function asyncParallel() { try { let res1

Fetch API - returned variable undefined

随声附和 提交于 2019-12-12 06:48:25
问题 I am a bit of a newbie to ES6 Javascript and have been trying to write a module to grab some data from the FourSquare API using fetch() and stick the results into some list items. The code for the module is below: export default (config) => class fourSquare { constructor(){ this.clientid = config.client_id; this.secret = config.client_secret; this.version = config.version; this.mode = config.mode; } getVenuesNear (location) { const apiURL = `https://api.foursquare.com/v2/venues/search?near=$

How to create a Promise for nested async calls

对着背影说爱祢 提交于 2019-12-12 04:12:54
问题 I have two functions which are starting asynchronous loading of resources. How can I make them return promises to that I can wait until the loading is finished? // #1 LoadMaps() { gFileService.getFile('syn-maps.json').then( mapItem => this.synMaps = mapItem ).then( gFileService.getFile('sense-maps.json').then( mapItem => this.senseMaps = mapItem ) ); } // #2 LoadListAndMetadata() { gListService.getList().then(lexList => { let promises = []; lexList.forEach(lexItem => { lexItem.selected =

How to use a generator function as a callback inside a promise returned by fetch call?

纵饮孤独 提交于 2019-12-12 03:37:05
问题 return fetch(url, { credentials: 'same-origin', ...options }) .then(response => response.json()) .then(function*(response) { console.log("httpStatusCode", response.httpStatusCode) }) Is the above possible ? I'm not getting the consoled output when the callback function is a generator, which means that the control is not passing to the callback function(generator). The real reason I want to do it this way is that I have to call another fetch request using the 'call' helper function of redux

How do I access previous promise results in a .then() chain?

浪子不回头ぞ 提交于 2019-12-12 02:49:14
问题 I have restructured my code to promises, and built a wonderful long flat promise chain , consisting of multiple .then() callbacks. In the end I want to return some composite value, and need to access multiple intermediate promise results . However the resolution values from the middle of the sequence are not in scope in the last callback, how do I access them? function getExample() { return promiseA(…).then(function(resultA) { // Some processing return promiseB(…); }).then(function(resultB) {

Determine which promise is slowest in Promises.all

耗尽温柔 提交于 2019-12-12 02:44:09
问题 I have been using Promise.all in my app. For the purpose of improving app speed, how to determine which promise is the slowest? const result = await Promise.all([ this.props.fetchUser(), this.props.cacheResourcesAsync(), this.props.initAmplitude(), this.props.initAppVariables(), ]); 回答1: You can use a helper function for that: async function time(p, name) { const start = Date.now(); try { return await p; } finally { const end = Date.now(); console.log(`${name} took ${end-start}ms`); } } Then