es6-promise

How to promisify Mongo/Mongoose find

不问归期 提交于 2020-06-01 05:07:46
问题 As part of another question I am trying to promisify a mongo/mongoose find query. I found little help via the search bar. The query is below, I am running this query as part of a controller in express. Setup is route -> userController.monitor which needs to contain the query In getting help for the other question I was asked to promisify find so that you can use await for it (like const incidents = Incident.find({fooID}).exec(); though SO search and my attempts at promisifying it myself have

How to promisify Mongo/Mongoose find

与世无争的帅哥 提交于 2020-06-01 05:06:07
问题 As part of another question I am trying to promisify a mongo/mongoose find query. I found little help via the search bar. The query is below, I am running this query as part of a controller in express. Setup is route -> userController.monitor which needs to contain the query In getting help for the other question I was asked to promisify find so that you can use await for it (like const incidents = Incident.find({fooID}).exec(); though SO search and my attempts at promisifying it myself have

How to save results of a promise to a variable? [duplicate]

試著忘記壹切 提交于 2020-05-31 07:10:19
问题 This question already has answers here : How do I return the response from an asynchronous call? (39 answers) Closed last year . I need to fetch some JSON data from an API and assign the result to a variable. I can see the array of data in the console, but [abc] is always set as a Pending promise. I've tried to assign [abc] instead of logging [data] or simply returning [data] but it's not working. Ideally, I would also want to stop the execution of the following code until I get the required

await loop vs Promise.all [duplicate]

假如想象 提交于 2020-05-26 04:51:29
问题 This question already has answers here : Any difference between await Promise.all() and multiple await? (5 answers) Closed 2 years ago . Having a set of async operations on db to do, I'm wondering what's the difference performance-wise of doing a "blocking" await loop versus a Promise.all . let insert = (id,value) => { return new Promise(function (resolve, reject) { connnection.query(`insert into items (id,value) VALUES (${id},"${value}")`, function (err, result) { if (err) return reject(err)

How JavaScript promises work behind the scenes

丶灬走出姿态 提交于 2020-05-22 03:52:46
问题 I'm so much confused about what happens behind the scenes when promise is produced and consume. Please clarify my points and sorry for my weak English. blank object is created with new keyword Promise constructor is called and new keyword set the this of Promise constructor points to the blank object this = blankobject. Promise constructor receives callback (executor function) in argument and calls the executor function. executor function receives two callbacks (resolve,reject) as arguments

How JavaScript promises work behind the scenes

余生颓废 提交于 2020-05-22 03:49:08
问题 I'm so much confused about what happens behind the scenes when promise is produced and consume. Please clarify my points and sorry for my weak English. blank object is created with new keyword Promise constructor is called and new keyword set the this of Promise constructor points to the blank object this = blankobject. Promise constructor receives callback (executor function) in argument and calls the executor function. executor function receives two callbacks (resolve,reject) as arguments

Resolve promise at a later time

好久不见. 提交于 2020-05-16 22:06:55
问题 I would like to construct a Promise, but defer resolution until later. The code below creates a promise, but it is resolved immediately. How can I control when the promise gets evaluated? var p = new Promise((resolve, reject) => { resolve(1); }) .then((p1) => { console.log(p1 + 1); }); UPDATE: To clarify, the reason for wanting to separate the declaration of the promise from its execution is to add then callbacks dynamically, based on some arguments. 回答1: You can pass resolve and reject to

Resolve promise at a later time

ⅰ亾dé卋堺 提交于 2020-05-16 22:06:24
问题 I would like to construct a Promise, but defer resolution until later. The code below creates a promise, but it is resolved immediately. How can I control when the promise gets evaluated? var p = new Promise((resolve, reject) => { resolve(1); }) .then((p1) => { console.log(p1 + 1); }); UPDATE: To clarify, the reason for wanting to separate the declaration of the promise from its execution is to add then callbacks dynamically, based on some arguments. 回答1: You can pass resolve and reject to

How do I defer an ES6 promise like jquery Deferred?

谁都会走 提交于 2020-05-14 19:49:51
问题 1. Using es6 promise, but the syntax is incorrect. I'm using es6, and want to make a deferred confirm dialog: // First, create an empty promise: let promise = new Promise((resolve, reject) => {}) // Then, show the dialog: let $dialog = $('#dialog-confirm').show(); // FAIL: I want to trigger the promise resolver, but failed. $dialog.find('.btn-yes').click(() => { promise.resolve(); }) $dialog.find('.btn-no').click(() => { promise.reject(); }) When I clicked the button, it failed, because the

How do I defer an ES6 promise like jquery Deferred?

一笑奈何 提交于 2020-05-14 19:49:33
问题 1. Using es6 promise, but the syntax is incorrect. I'm using es6, and want to make a deferred confirm dialog: // First, create an empty promise: let promise = new Promise((resolve, reject) => {}) // Then, show the dialog: let $dialog = $('#dialog-confirm').show(); // FAIL: I want to trigger the promise resolver, but failed. $dialog.find('.btn-yes').click(() => { promise.resolve(); }) $dialog.find('.btn-no').click(() => { promise.reject(); }) When I clicked the button, it failed, because the