Promise object not returning a value

return value from promise
get value from promise javascript
object promise to string
promise.resolve is not a function
how to get promise value fetch
return new promise
promise then
wait for promise to resolve

I am trying to get the value of asset:

const asset = getAssetInformation(11002);

function getAssetInformation(id) {
    return axios({
        method: 'GET',
        url: "/asset-information",
        params: {assetId: id}
    });
}

console.log(asset);

The result is:

[object Promise]

How do I get the returned value from the request?

Using async and await is a practical way -

function axios(query) { // fake axios, for demo
  return new Promise (r =>
    setTimeout(r, 1000, { asset: query.params.assetId })
  )
}

function getAssetInformation(id) {
  return axios({
    method: 'GET',
    url: "/asset-information",
    params: {assetId: id}
  })
}

async function main() { // async
  const asset = await getAssetInformation (11022) // await
  console.log(asset)
}

main()
// 1 second later ...
// { asset: 11022 }

Promise.resolve(), If the value is a promise, that promise is returned; if the value is a var p2 = Promise.resolve(thenable); p2.then(function(v) { // not called }  It represents its eventual value. So getImageMeta () is immediately returning the promise even though it has not resolved. Once you have the promise returned by getImageMeta () you can use its then () to wait for the final boolean, which will come on the next event loop cycle at the earliest.

You will need to use .then function.

const asset = getAssetInformation(11002)
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

Promise.prototype.then(), const promise1 = new Promise((resolve, reject) => { If it is not a function, it is internally replaced with an "Identity" function (it returns the returns a value, the promise returned by then gets resolved with the returned value as  1. Return nothing --> PromiseB is resolved immediately, and undefined is passed to the success handler of promiseB 2. Return a value --> PromiseB is resolved immediately, and the value is passed to the success handler of promiseB 3. Return a promise --> When resolved, promiseB will be resolved. When rejected, promiseB will be rejected.

Promises return another promise.

The way to unpack and work with a promise is through the .then() function, which will run after the promise returns.

const asset = getAssetInformation(11002);

function getAssetInformation(id) {
    return axios({
        method: 'GET',
        url: "/asset-information",
        params: {assetId: id}
    });
}

Asset here is a promise, you want to use a then on it to get the value.

Instead of..

const asset = getAssetInformation(11002);

use

getAssetInformation(11002)
.then((response) => {
//What you returned will be here, use response.data to get your data out.

} )
.catch((err) => {
//if there is an error returned, put behavior here. 
})

Promises chaining, await stops executing until the Promise has resolved (ie, has a value). Example function that returns a Promise that will resolve after 2 seconds var result) // But the best part is, we can just keep awaiting different stuff, without ugly .then()s  Promises don't "return" values, they pass them to a callback (which you supply with.then ()). The spec simply sounds confused to me. It's probably trying to say that you're supposed to do resolve ([genre1, genre2,]); inside the promise implementation. – Ixrec Apr 22 '15 at 23:07

How do I make a JavaScript promise return something other than a , In this code, we add a callback on the promise, then we return the promise. The result of promise.then is a lost promise because no one can interact with it. want to create an auto-resolved promise with a simple value, use Promise.​resolve : The rule is, if the function that is in the then handler returns a value, the promise resolves/rejects with that value, and if the function returns a promise, what happens is, the next then clause will be the then clause of the promise the function returned, so, in this case, the first example falls through the normal sequence of the thens and prints out values as one might expect, in the second example, the promise object that gets returned when you do Promise.resolve("bbb")'s then is the

Traps of promises, A promise is defined as an object that has a function as the value for the property Now, in an asynchronous world, you can no longer return values: they simply  The Promise object represents the it cannot transition to any other state and it’s value will not change as well. When a Promise is Let’s transform this object to only return names.

You're Missing the Point of Promises · GitHub, If value is not a thenable, a fulfilled Promise is returned with value as its fulfillment value. If value is a thenable (Promise-like object, like those returned by  If the value is a thenable (i.e. has a then method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value. Generally, if you don't know if a value is a promise or not, Promise.resolve (value) it instead and work with the return value as a promise.

Comments
  • Using promises - JavaScript | MDN
  • Possible duplicate of Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
  • @zero298 that is irrelevant here
  • @user633183 I disagree. This is an issue with not understanding how to Promise, an asynchronous primitive. That question details how to use Promise in detail.
  • @zero298 the asker is not trying to alter a variable - Sure, this is a very common mistake when dealing with asynchronous values, but it's not a mistake the asker is making here.