getId Cannot read property 'then' of undefined . But async/await would have made it clearer that you have to set state in the the callback because the value doesn't exist until the promise returns. Modified 3 years, 11 months ago. While a Promise object is "pending" (working), the result is undefined. If we are now returning anything inside two, TypeScript will replace the previous Hey with an undefined. That's very convenient in practice. It behaves the same as calling Promise.prototype.then(undefined, onRejected) (in fact, calling obj.catch(onRejected) internally calls obj.then(undefined, onRejected)). Target is to fetch pets from api and if pet not exist to return altered array with only existing pet names. :P. Thanks in advance for your help! From Node Lecture Asynchronous JavaScript : 8- Consuming Promises Hello Forum, I'm confused why the third then( ) always return undefined. One is incorrect. When a Promise is created, it will be pending. A promise represents the eventual result of an asynchronous operation. Your code proceeds . When a Promise object is "rejected", the result is an . Promise then(.) There are a number of copies of this issue floating around and most of them are labeled as a dupe of #4260 , however on cursory glance of that issue I think perhaps the reality is that #4260 is a a prerequisite before fixing this issue. Click an available time slot on the calendar below to reserve a room. If the new Promise resolves, then two will also resolve by taking the new Promise 's resolved value as its own. Description The static Promise.resolve function returns a Promise that is resolved. with return countQuery.then (.). to see whether the rejected promise is caught. You want to do it prior (but there's more, keep reading): let throttleAsync = function(url) { return promise.then(() => { It allows us to call the next .then method on the new promise. In Example 2, the getGithubOrgs(url) function calls the Fetch API, which returns a Promise that resolves to a response object. It is just that resolve is not a valid function on the promise object. I know the query returns data when the function executes, it however does not seem to pass that data to the actual return part of the function call. Mar 16, 2022 P Paulie Guest Mar 16, 2022 #1 Paulie Asks: Firebase Promise Returning Undefined Data Javascript I've followed several guides on how to correctly wait for the data from my ListFile () function. How to return data from promise; Export Cookie Jar to JSON with Node Request; node.js call external exe and wait for output; node.js resolve promise and return value; How can I send an object to a file, process that object, return and get the result because I'm missing something here and I do not understand why I get undefined and my Promise . 1 People found this is helpful async-await javascript node.js Advertisement The catch() method returns a Promise and deals with rejected cases only. Cleaner, simpler. The JavaScript language Promises, async/await June 18, 2022 Error handling with promises Promise chains are great at error handling. archy-bold commented on Sep 6. Promise.reject (reason) Returns a new Promise object that is rejected with the given reason. change var promise = to return . If the returned promise fulfills, it is fulfilled with the value of the first promise in the iterable that fulfilled. A promise can be one of the three states below. If a promise has succeeded or failed and you later add a success/failure callback, the correct callback will be called, even though the event took place earlier. code to use result .}) However, in the promise completion block it's always undefined. Promise.all() return undefined. This makes the promise created by thenresolve with the value undefined. W3Guides. To make the function promisedDivision (n1, n2) return a rejected promise let's set the second argument to 0. So const api will always equal undefined. I know the query returns data when the function executes, it however does not seem to pass that data to the actual return part of the function call. Try it Syntax Promise.all(iterable) Parameters iterable An iterable (such as an Array) of promises. You'll have to change the way you code. export function getHotOffers () { let offerPromise = getRequest ('/hot-offers') .then (offers => JSON.parse (offers)); return offerPromise; } Similarly, any code that calls this will be getting a promise and will have to use its .then method to get the final value. Promise.resolve(r) fetchIDs (GET ). I hope that was clear enough! Home Web Design Programming Languages Database Design and Development Software Development Tools Artificial Intelligence Mobile Development Computer Science. When calling a function that returns a promise, comes back as undefined unless async operators are removed, then returns ZoneAwarePromise, but contains no data. edited to join this conversation on GitHub . I had to add a folder for the scoped module under the root mocks folder ie __mocks__/@module/file.js. You will need to use the promise to get access to the offers. A JavaScript Promise object can be: Pending; Fulfilled; Rejected; The Promise object supports two properties: state and result. Share Follow answered Aug 18, 2015 at 6:49 Matt Way 31.4k 10 77 83 4 Thanks Matt - your suggested change makes it work as expected. If it rejects, it is rejected with the reason from the first promise that was rejected. Return value A Promise that is resolved with the given value, or the promise passed as value, if the value was a promise object. To do that there is two popular way described below. This happens because after making a call to getResult method, it in turns calls the getPromise method which gets resolved only after 2000 ms. getResult method doesn't wait since it doesn't returns a promise. Only works in IE 11 if bluebird.min.js script element is uncommented. function fetchIDs {. The first state is called the Pending state. throws an error, the promise returned by then gets rejected with the thrown error as its value. Your test function does return a undefined. Use of setTimeout () function: In order to wait for a promise to finish before returning the variable, the function can be set with setTimeout (), so that the function waits for a few milliseconds. Viewed 3k times -1 I have have array with three pet names. If a function returns a Promise, it means that the result of the function call is not available. to be compatible with ES5. You get a promise of a result. you're currently immediately resolving your outer promise but with an inner promise, and that inner promise doesn't return anything at all, hence undefined. It cannot succeed or fail twice, neither can it switch from success to failure or vice versa. I think I know why, because I return the Axios within an async function. Instead of "get the id, then do something with it", you need to "do something whenever the id is loaded/changed". Finally, then() returns a new Promise that resolves to JSON. This promise will have the state as pending and result as undefined. Promise resolver undefined is not a function at new Promise (<anonymous>) The fix is straightforward: you must provide a way to resolve or reject promises: // Instead of this const promise = new Promise() // do this const promise = new Promise(() => {}) That will fix the problem. Runs without error in IE11. Therefore, the correct way to get an array of promises is as follows: This means you will get undefined as the return value of apiGetAll. [Solved]-NodeJS - Promise returns undefined object inside a redis pool await-node.js score:0 Accepted answer in your attempts you use await configPool.get ('allImages', async (err, reply) => { firstly, .get doesn't return a promise, so await ing it makes no sense secondly, you're not using await inside the callback, so no need to make it async When the first .then method returns a value, the next .then method can receive that. You cannot return data that is being loaded asynchronously. The promise.then() call always returns a promise. Promise Object Properties. When calling a function that returns a promise, comes back as undefined unless async operators are removed, then returns ZoneAwarePromise, but contains no data. doesn't return anything, the promise returned by then gets resolved with an undefined value. This resulting Promise is received by the then() method, which parses the response to JSON using the json() method. - Jaromanda X Jun 22, 2017 at 6:36 Problem is that Promise.all() return undefined. None of the above worked and in my case the problem was that I was adding a mock in a __mocks__ directory next to the file, but the import used a 'scoped module'. Your title asks why new Promise is returning undefined, when the fact is that it isn't. It is indeed returning a valid promise. Fails in IE 11 with error SCRIPT5009: 'Promise' is undefined. If not let mw know! You need to return a promise for the result: function test(p) { return Promise.all(ccxt.exchanges.map(api => { //Looping . For instance, in the code below the URL to fetch is wrong (no such site) and .catch handles the error: returns an already fulfilled promise, the promise returned by then gets fulfilled with that promise's value as its value. Already have an account? For that you need to use the original Promise constructor. But how can I use .then () to get and use the response in the vue file. It may be either fulfilled or rejected for example, resolving a rejected promise will still result in a rejected promise. JavaScript - Cannot read property 'then' of undefined. Use of setTimeout () function. I'm trying to get a value back from this promise but it keeps returning undefined on my template.If you look at my getLabel function, I am getting the label in the argument which is a promise. The argument is a function providing two arguments: a resolve and . Use of async or await () function. And we can not access the real output when it is available using the Promise return by the function. The second one can now pass to the third .then() and so on. always return 'undefined' Node. stopAll () { startmeetingApi.stop ().then ( (res) => { this.transcript = res.data.transcript; }); console.log (this.transcript . Just return the promise directly. The reason it is returning undefined is because get_message () is not returning anything.