es6-promise

Promise within while loop won't run - causing infinite loop

为君一笑 提交于 2021-02-11 15:56:20
问题 I am trying to run a Promise within a while loop. The code after the promise (in the .then) will eventually break the while loop. However, the promise never runs. The while loop just keeps running infinitely without triggering the promise. Why is this? Is it impossible to use a promise in a while loop? A simplified version of code is below while(relevantRefundRequests.length >= waitList[0].quantity){ stripe.paymentIntents.create({ }) .then(data => { ***code that will break while loop }) } 回答1

Promise within while loop won't run - causing infinite loop

北战南征 提交于 2021-02-11 15:55:53
问题 I am trying to run a Promise within a while loop. The code after the promise (in the .then) will eventually break the while loop. However, the promise never runs. The while loop just keeps running infinitely without triggering the promise. Why is this? Is it impossible to use a promise in a while loop? A simplified version of code is below while(relevantRefundRequests.length >= waitList[0].quantity){ stripe.paymentIntents.create({ }) .then(data => { ***code that will break while loop }) } 回答1

JS Promises: if a handler in a `then` block returns a value vs returning a resolved promise, does the `then` block handle it the same way?

﹥>﹥吖頭↗ 提交于 2021-02-11 14:01:05
问题 Say I have a function that returns a resolved promise like this: let a = () => {return new Promise(res => res(1))} and then I then-ify it like this: a() .then(val => {return new Promise(res => res(1))}) Here the then contains a handler that returns a promise resolved with 1 , so the then block returns a promise resolved with 1 as well. Is that right? Then say instead we have this: a() .then(val => {return 1}) The handler returns 1 instead of returning a promise resolved with 1 . What I Want

How to ensure the Jest bootstrap file is run first?

青春壹個敷衍的年華 提交于 2021-02-11 13:58:47
问题 Introduction I am getting stuck with database Promises I am using inside the Jest testing framework. Things are running in the wrong order, and following some of my latest changes, Jest is not finishing correctly because an unknown asynchronous operation is not being handled. I am fairly new to Node/Jest. Here is what I am trying to do. I am setting up Jest inside a multiple Docker container environment to call internal APIs in order to test their JSON outputs, and to run service functions in

How to ensure the Jest bootstrap file is run first?

陌路散爱 提交于 2021-02-11 13:58:19
问题 Introduction I am getting stuck with database Promises I am using inside the Jest testing framework. Things are running in the wrong order, and following some of my latest changes, Jest is not finishing correctly because an unknown asynchronous operation is not being handled. I am fairly new to Node/Jest. Here is what I am trying to do. I am setting up Jest inside a multiple Docker container environment to call internal APIs in order to test their JSON outputs, and to run service functions in

How do nested Javascript returns “pass up” promises?

天大地大妈咪最大 提交于 2021-02-11 12:23:14
问题 This question has two parts, both relating to how Javascript promises are passed around functions using return statements. 1) I have simple Javascript function, which includes multiple return statements. The inner function returns a promise to an arrow function, the arrow function is also returned, like this: const returnMe(data){ return () => { return Promise.resolve(data); }; }; Could I write the following code? returnMe("Hello!").then((msg) => { console.log(msg) }); /// --> output "Hello!"

Error in Promise Not sending any error in response object in NodeJS/Express/Mongoose

让人想犯罪 __ 提交于 2021-02-10 14:24:09
问题 I am trying to convert old mongoose promise code to Native ES6 Promise. I am getting all the errors thrown in catch and I can log it in console but when I try to pass it to response object I get empty object. Following is my code module.exports.login = function(req, res) { var userPromise = User.findOne({email:req.body.email}).exec(); userPromise.then(function(user) { if(!user){ throw new Error("step 1 failed!"); } }) .then(function(user) { if(!user.comparePassword(req.body.password)){ throw

Error in Promise Not sending any error in response object in NodeJS/Express/Mongoose

江枫思渺然 提交于 2021-02-10 14:23:40
问题 I am trying to convert old mongoose promise code to Native ES6 Promise. I am getting all the errors thrown in catch and I can log it in console but when I try to pass it to response object I get empty object. Following is my code module.exports.login = function(req, res) { var userPromise = User.findOne({email:req.body.email}).exec(); userPromise.then(function(user) { if(!user){ throw new Error("step 1 failed!"); } }) .then(function(user) { if(!user.comparePassword(req.body.password)){ throw

“await this.method();” doesn’t work in static method

拜拜、爱过 提交于 2021-02-08 13:45:24
问题 I know of the ES6 await feature and I’d like to use it in a function I created in a class. It works well, but when the function is a static function, it doesn’t. Is there any reason for that? Also, what will be the right way to be able to use await inside a static function? class MyClass { resolveAfter2Seconds() { return new Promise(resolve => { setTimeout(() => { resolve('resolved'); }, 2000); }); } static async asyncCall() { console.log('calling'); var result = await this

Express middleware, next and Promises

对着背影说爱祢 提交于 2021-02-08 13:41:30
问题 There is very simple Express router with handler: router.get('/users/:userId/roles/:roleId', function(req, res, next){ const roleId = req.params.roleId; res.rest.resource = UserModel.findOne({ _id: req.params.userId}).exec().then(function(usr) { console.log(req.params.roleId); // => undefined console.log(roleId); // => okay here const result = usr.roles.find( role => String(role._id) === String(roleId)); return result; }); next(); }); As it seen accessing req.params.roleId within promise