es6-promise

ES6 promise execution order

不打扰是莪最后的温柔 提交于 2019-12-19 09:58:40
问题 I would expect the output for the following snippet to be 1, 2, 3, 4 . But, the actual output order is 1, 4, 3, 2 . self.promiseChain = new Promise(function (resolve, reject) { setTimeout(resolve, 4000); }).then(function () { console.log(1); }); self.promiseChain.then(function () { return new Promise(function (resolve, reject) { setTimeout(resolve, 3000); }).then(function () { console.log(2); }); }); self.promiseChain.then(function () { return new Promise(function (resolve, reject) {

Why do both Promise's then & catch callbacks get called?

爷,独闯天下 提交于 2019-12-19 05:14:30
问题 I have the following code and when it's executed, it returns both " rejected " and " success ": // javascript promise var promise = new Promise(function(resolve, reject){ setTimeout(function(){reject()}, 1000) }); promise .catch(function(){console.log('rejected')}) .then(function(){console.log('success')}); Could anyone explain why success is logged? 回答1: The then callback gets called because the catch callback is before it, not after. The rejection has already been handled by catch . If you

Can I use other promise implementations in the Parse JavaScript SDK?

谁说胖子不能爱 提交于 2019-12-18 14:56:02
问题 I am concerned about references I have seen to Parse using JQuery-compatible promises, as I have read that jQuery promises allow consumers to mutate the state of the promise. Is it possible to use another promise implementation that is known to be Promises/A+ compliant (e.g. the ECMAScript 6 implementation, or Bluebird) with the Parse JavaScript SDK? Normally I would assume that this isn’t possible, but in v1.4.2 of the Parse JavaScript SDK, the implementation of Parse.Promise defines the

Intellij Idea warning - “Promise returned is ignored” with aysnc/await

一个人想着一个人 提交于 2019-12-18 13:54:12
问题 I'm using Express.js in my code with Node.js v7.3. In this I've created a User Router which forwards the requests to my User Controller . I'm using async/await inside the User Controller to do asynchronous calls. The problem is that IntelliJ gives me a warning saying that Promise returned from login() is ignored. The thing is I'm not even returning anything from the login() method. Here's the code - UserRouter.js router.post('/login', function (req, res, next) { userController.login(req, res)

How does jest.fn() work

本秂侑毒 提交于 2019-12-18 11:44:55
问题 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

How to determine if a Promise is supported by the browser

我是研究僧i 提交于 2019-12-18 10:40:37
问题 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. 回答1: 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

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

泄露秘密 提交于 2019-12-18 09:47:44
问题 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,

Async await strange behaviour with functions

时间秒杀一切 提交于 2019-12-18 09:26:14
问题 I have following asynchronous code example: // Functions function getSomePromise() { let a = new Promise((resolve, reject) => { setTimeout(function(){ console.log("Inside promise..."); resolve("Success!"); }, 1000); }); return a; } async function someWrapper(i) { console.log('A: '+ i); await getSomePromise(); console.log('B: ' + i); } And two tests: async function test1() { for(let i=0; i<5; i++) { // body copy-pasted of someWrapper function: console.log('A: '+ i); await getSomePromise();

Rejecting Javascript Promises And Error Handling

血红的双手。 提交于 2019-12-18 08:40:20
问题 I'm trying to wrap my head around the correct way to indicate a failure within a .then() . If the promise doesn't fail, (I.e. the operation that returns the promise does it' job properly, such as an AJAX request that returns a status 200), but I decide that the result isn't valid, usually I'd do a popup, explaining the issue to the user, and do a "return false;" to exit the method early. However, with promises, if from within the .then(), I want to do something similar, I've been lead to

Rejecting Javascript Promises And Error Handling

泪湿孤枕 提交于 2019-12-18 08:40:07
问题 I'm trying to wrap my head around the correct way to indicate a failure within a .then() . If the promise doesn't fail, (I.e. the operation that returns the promise does it' job properly, such as an AJAX request that returns a status 200), but I decide that the result isn't valid, usually I'd do a popup, explaining the issue to the user, and do a "return false;" to exit the method early. However, with promises, if from within the .then(), I want to do something similar, I've been lead to