Why does Chrome console return Promise object from .then() instead of the resolved value? [duplicate] - javascript

This question already has answers here:
Why does Promise.prototype.then always return promises?
(2 answers)
How can I access the value of a promise?
(14 answers)
Does then() really return a promise in all cases? [duplicate]
(2 answers)
promise.then() returns Promise { <pending> } [duplicate]
(3 answers)
Closed 7 months ago.
I'm trying to better understand promises and async code in Javascript.
In doing so, I wrote the following, simple async function in the Chrome console:
const myAsync = async() => 10;
As expected, myAsync returns a promise, but when I call .then() (i.e. myAsync().then(res => res)), then console displays Promise{<fulfilled>: 10}. The promise is clearly fulfilled, but I would expect it to display, simply, the value 10.
Interestingly, if I modify the body of my call to .then() to res => alert(res), the alert displays what I expect, just 10.

Related

How to use biri in vanilla js? [duplicate]

This question already has answers here:
Async function returning promise, instead of value
(3 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 months ago.
I am trying to use biri on my browser (vanilla javascript) I am following the tutorial but I have an error saying I can't use the await statement.
I've tried to create a async function to return the uniqueId but it return a promise object. I am struggle since more than a hour to get this uniqueId.
Any suggestion ?

A variable inside async function derived from a promise has different value when returned to global scope [duplicate]

This question already has answers here:
async/await implicitly returns promise?
(5 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
I do not understand why the query variable has a value inside async function that changes when returned and declared in the global environment. I want to return the value from my database to a global variable outside of the async function.
What is the logically reason this happens. How do I extract the integer value from the Promise {1}.
async function glob(){
var result = await knex.raw('select max(glob) from or');
let query = await result.rows[0].max ;
console.log(query);
return query ;
}
let go = glob();
setTimeout(()=>{console.log(go)},1000);
setTimeout(()=>{console.log(go == 1)},1000);
The console returns.
1
Promise { 1 }
false

Return [[PromiseResult]] in vue js [duplicate]

This question already has answers here:
How can I access the value of a promise?
(14 answers)
Is there a way to save [[Promise Result]] as a variable? [duplicate]
(3 answers)
Is it impossible to get the [[PromiseValue]] from a Promise object without using the Promise object's then method? [duplicate]
(1 answer)
Closed 2 years ago.
How to return [[PromiseResult]] guys, please help thankss
i use vue js and laravel
You can get that result by adding then method to the Promise. For example:
new Promise((resolve, reject) => resolve('success')).then(res => console.log(res))
This code will log your [[PromiseResult]] object.

Why is this Promise object's status 'fulfilled'? [duplicate]

This question already has answers here:
Different property value is displayed when JavaScript object is expanded in Chrome console
(2 answers)
Weird behavior with objects & console.log [duplicate]
(2 answers)
Can't access object property, even though it shows up in a console log
(36 answers)
Closed 2 years ago.
If asynchronous functions execute after the main thread, then why does k, printed to the console, have [[PromiseStatus]]: "fulfilled". Surely a promise can only be fulfilled when it's successfully completed its asynchronous behaviour. Shouldn't it be 'pending' or something? It does say pending somewhere if you look at the picture.
function fetchDog(){
k = fetch("https://dog.ceo/api/breeds/image/random")
console.log(k);
};
fetchDog();

Conditional Promise [duplicate]

This question already has answers here:
Javascript - if with asynchronous case
(1 answer)
if-else flow in promise (bluebird)
(3 answers)
Closed 5 years ago.
I'm tying to understand how I can have a conditional promise, without repeating everything inside the .then function again.
For example:
if (cameraFilmType == "Standard")
Then I don't want to call the addImageFilter function
if (cameraFilmType == "Vintage")
Then i do want to call the addimageFilter function.
I still want to run everything inside the .then in both cases, but i need the option to use .then as the addImageFilter function is async. How can i do this without repeating the code in .then twice?
factory.addImageFilter(imageData, cameraFilmType).then(function (newImg) {
$scope.imageData = newImg;
//Do loads of stuff
});

Categories