es6-promise

Data from ES6 promise doesn't render on the page until I click on it?

孤街浪徒 提交于 2021-02-05 11:27:27
问题 I an using Ionic for my app with a connection to Firebase to pull data. I created a promise in a factory to pull data down and thought it should render the data on the screen once it finishes but I'm getting nothing until I touch the screen? I get no error and the data does indeed come. Factory: all: function () { return new Promise ((resolve, reject) => { firebase.database().ref('desks').once('value').then(snapshot => { let desks = [] snapshot.forEach((child) => { desks.push(child.val()); })

Unable to resolve promise rejection and send array as response

守給你的承諾、 提交于 2021-02-05 08:23:26
问题 I am trying to handle two queries within each other. exports.get_users = (req, res) => { SubscriptionPlan.find() .then((result) => { if (!result) { return res.status(400).json({ message: "unable to process" }); } let modifiedData = []; result.forEach(async (data) => { if (data.processStatus === "active") { await Users.findById(data.userId).then( (response) => { console.log(response) modifiedData.push(response); res.json(modifiedData) } ); } }); }) .catch((err) => console.log(err)); }; My

Returning Promise with an async function

二次信任 提交于 2021-02-04 21:01:36
问题 Given the following two implementations (in ES6 / NodeJS) async TestFunc() { return new Promise((resolve,reject) => { ... }); } and TestFunc() { return new Promise((resolve,reject) => { ... }); } Is there any difference in behavior if I were to call either of these functions like so? await TestFunc(); I would assume that the first (async) implementation would return a promise, and we would be awaiting that to return another promise, whereas the latter (synchronous) implementation would return

How to chain nested promises containing then and catch blocks?

℡╲_俬逩灬. 提交于 2021-01-29 09:48:43
问题 How to chain ES6 nested Promises with each Promise in the nesting having then and catch blocks? For example, what will be the Promise then and catch blocks chaining implementation equivalent for following nested AJAX calls implementation handled by success and error callbacks, considering every API call returns a Promise ? $.ajax({ url: 'url1', success: function() { console.log('URL1 call success'); $.ajax({ url: 'url2', success: function() { console.log('URL2 call success'); }, error

Fetch in Internet Explorer?

放肆的年华 提交于 2021-01-29 09:22:53
问题 Following code does not work on IE at all. fetch("{{{LINK TO API}}}", { headers: { "Accept": "application/json", "Content-Type": "application/json" } }) .then(resp => resp.json()) .then(function(json) { // SET VARIABLES var seller = json.sellers[Math.floor(Math.random() * Math.floor(json.sellers.length))]; // INSERT COMPANY LOGO companyLogo.src = json.logo_url; // INSERT SELLER PROFILES document.querySelectorAll("[data-seller-profile]").forEach(wrapper => { var innerHTML = ""; innerHTML += "

How to correctly resolve a promise within promise constructor

倖福魔咒の 提交于 2021-01-29 08:50:30
问题 const setTimeoutProm = (delay) => new Promise(res => setTimeout(() => res(delay),delay)) I want to do something like, const asyncOpr = (delay) => { return new Promise((resolve, reject) => { //update delay for some reason. const updatedDelay = delay * 2; setTimeoutProm(updatedDelay).then(res => { resolve(res); }).catch(err => {}) }) } asyncOpr(2000).then(() => alert("resolved")) //this works This works as expected, but I am not sure if this is correct way of doing this or is there any better

Reject a promise and stop timeout on socket-io event

删除回忆录丶 提交于 2021-01-29 07:18:40
问题 I'm currently using the following function to simulate awaiting (sleep) in my Node.js server script. function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } and I'm using it like below. When used in this way, executing the code below that line is paused until 20 seconds have been passed. await sleep(20000) Now I want to break this sleeping on a socket-io event , but I can't figure a correct way out to do that. I tried the following code, but it didn't break the sleep. /

React Native - [Unhandled promise rejection: Error: Signup Error]

我的梦境 提交于 2021-01-29 05:17:26
问题 I get this intentional error because I'm using the same email to register. It says I'm not handling the rejection; can someone explain why because I have a .catch() which should catch any errors that were thrown Error code: [Unhandled promise rejection: Error: Signup Error] - node_modules\promise\setimmediate\core.js:37:14 in tryCallOne - node_modules\promise\setimmediate\core.js:123:25 in setImmediate$argument_0 - node_modules\react-native\Libraries\Core\Timers\JSTimers.js:146:14 in

JS promises: is this promise equivalent to this async/await version?

大城市里の小女人 提交于 2021-01-29 05:12:49
问题 If I have the following code new Promise(res => res(1)) .then(val => console.log(val)) is this equivalent to let val = await new Promise(res => res(1)) console.log(val) I know one difference is that I have to wrap the second one in an async function, but otherwise are they equivalent? 回答1: Because your promise always resolves (never rejects), they are equivalent. You could also do: Promise.resolve(1).then(val => console.log(val)); Keep in mind that a major difference with await (besides it

JS promises: is this promise equivalent to this async/await version?

…衆ロ難τιáo~ 提交于 2021-01-29 05:08:53
问题 If I have the following code new Promise(res => res(1)) .then(val => console.log(val)) is this equivalent to let val = await new Promise(res => res(1)) console.log(val) I know one difference is that I have to wrap the second one in an async function, but otherwise are they equivalent? 回答1: Because your promise always resolves (never rejects), they are equivalent. You could also do: Promise.resolve(1).then(val => console.log(val)); Keep in mind that a major difference with await (besides it