I'm struggling to extract the string "foo" from my Promise. I'm starting to think that my Promise declaration is incorrect.
const bar = new Promise((res, rej) => res("foo"));
then((result) => {
return result;
});
I'm struggling to extract the string "foo" from my Promise. I'm starting to think that my Promise declaration is incorrect.
const bar = new Promise((res, rej) => res("foo"));
then((result) => {
return result;
});
Here is the recommended approach:
const variable = new Promise((resolve, reject) => resolve("value"));
variable.then((result) => {
console.log(result);
// perform any desired actions with result
}).catch(error=>console.log(error))
The then
function gets executed upon successful resolution of the promise.
The catch
method triggers when the promise is rejected.
const example = new Promise((resolve) => resolve("hello"))
.then((output) => {
console.log(output)
});
👉 Remember to include a period (.
) before the word then
-> .then
then
is a method of the Promise
object, so you must use .then
to invoke the method. then
cannot be used independently as a global function.
When you write:
new Promise((resolve) => resolve("hello")).then((output) => ...
the then
will only execute upon successful resolution of the Promise.
The Promise internally tracks all then
methods and triggers them when you call resolve("hello")
(in this case), hence the need for .then
- to register those callbacks to be executed upon resolution
When you assign the Promise to the variable bar
, make sure to use then
to retrieve the value like this:
const bar = new Promise((res, rej) => res("foo")).then((result) => {
console.log(result)
});
It's also important to include a catch
statement for error handling.
If you're looking to learn more about Promises, check out this free course on Udacity from Google to improve your understanding.
I have two different arrays as shown below: const firstArray = [{name: "1"}, {name: "2"}, {name: "3"}, {name: "4"}, {name: "4"}]; const secondArray = [{name: "1"}, {name: "5"}, {name: &q ...
Currently, I am in the process of learning JavaScript and experimenting with creating games to make the learning experience more enjoyable. However, I am facing an issue while using EaselJS and CreateJS to develop these games as a beginner. I need some as ...
Recently, I encountered an issue with a function that displays a dialog box to ask users if their checks printed correctly. Upon clicking on another check to print, the "checked_id" value remains the same as the previously executed id. Surprisingly, this i ...
In the following code snippet, I am iterating through multiple URLs and extracting data (title and content) from each of them. My objective is to store this data for later use on another page, and thus it needs to be accessible using its respective title, ...
As a beginner with Fetch API and Promises, I have encountered an issue that I hope someone can help me with. My code involves fetching event data with a token, extracting team ids, and using these ids to fetch more information from another endpoint. Every ...
Can someone help me with resizing a pop up? I've been struggling to get it right. This is the popup template in question: <ion-view> <ion-content scroll="false" class=""> test </ion-content> < ...
I am currently facing an issue while trying to populate a Scatter plot in JavaScript with decimal values obtained from a MySQL database using PHP. The database contains around 150,000 entries, resulting in 150,000 pairs of decimal inputs for the graph. How ...
Currently, I am attempting to retrieve images from Firebase storage and then exhibit them in a React component. As a newcomer to React/Javascript, I find it challenging to grasp the asynchronous nature of React/JS. The issue I'm facing is that althoug ...
While developing my Shopify App using Koa and Nextjs, I encountered the following error: appbridgeError: APP::ERROR::INVALID_CONFIG: shopOrigin must be provided The behavior of the app is a bit odd as it works fine when accessed for the first time. Howev ...
I'm currently attempting to execute the following code in Microsoft Edge using WebDriver ExpectedCondition<Boolean> jsLoad = driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals(&quo ...
During development, I implement a middleware called cors using the following syntax: app.use(cors({origin: 'http://localhost:8100'})); However, I have noticed that for every route request, two requests are being made as shown in the image below ...
I am experiencing an issue with my React website where I am using the withStyles feature to apply styles to a material-ui Grid element. Specifically, when attempting to use direction: "column" in the style, I encounter the error message provided below. Th ...
Given an array of objects, I use the reduce method to transform it into a new format where each key represents a date from the original array. For example, if the array contains objects with dates like {date: '22-02-06-00:55:66', name: 'one& ...
Upon page load, an ajax modal pop-up is displayed and then hidden once the loading process is complete. This functionality is defined within the master page. The issue arises when certain button clicks fail to trigger the window.onload javascript event, as ...
I'm working on a webpage that has a title at the top, but I want to hide it on smaller devices and make it responsive when the window is resized. Currently, I only show the title when the window width (w) is greater than 450 pixels. However, I'm ...
I have successfully imported a house model in .gltf format. I am now trying to extract the floor object from this model and turn it into its own individual a-frame entity for future reference. This is necessary so that I can designate the floor as the coll ...
How can I use SQLite in HTML5 to execute a call like the one below, so that the result is returned immediately rather than passed off to another function? try { mydb.transaction( function(transaction) { transaction.executeSql( &apo ...
I'm having trouble importing OrbitControls into my project. I included three.js and then attempted to import OrbitControls, but it's not functioning as expected. I added three.js to the HTML body using the following command: script src="https:/ ...
I'm encountering errors while trying to implement Set() in my Grocery Shopping app using MobX with TypeScript. I have a simple setup with defined types in types.ts: types.ts export type Item = { name: string; instock: number; price: number; }; ...
I'm currently facing a challenge that I could use some help with... While experimenting with Web SQL Databases, I've encountered an issue when trying to create a loop that functions correctly. Here's the code snippet I'm using: for ...