Promise returning undefined in Node.js | async function Node.js

Introduction

How to resolve a promise in Node.js? This is a very basic programming technique for a JavaScript developer in Node.js. In this blog post, you will learn best practices to use the result of asynchronous functions. What is the impact of not using await in Node.js.

In Node.js, async function always returns a promise object. To obtain the underlying value of the promise, you should use the await operator or call the then method. Attempting to use a promise object instead of its underlying value can lead to an unexpected result.

Example in Node.js

In the following example, the getResult the function returns a promise, and in the caller function there is a check if the returned promise is undefined:

async function getResult(id) {
  let req = await fetch(`https://topcode.in/data?id=${id}`);
  if (!req.ok) return null;
  return req.json();
}

async function foo(id) {
  let data = getResult(id);       //storing result of async function without await or then() 
  if (!data) {
    console.log("No data for: " + id);
    return;
  }
}

Recommendation

Use the await operator to get the value contained in the promise. Alternatively, you can call then method on the promise and use the value passed to the callback.

Following is the solution of the problem:

async function getResult(id) {
  let req = await fetch(`https://topcode.in/data?id=${id}`);
  if (!req.ok) return null;
  return req.json();
}

async function foo(id) {
  let data = await getResult(id);       //here using await keyword 
  if (!data) {
    console.log("No data for: " + id);
    return;
  }
}

References

#promise undefined nodejs #promise is not defined node js #nodejs promise returns undefined #promise undefined node js promise undefined js #promise result undefined javascript #uncaught (in promise) undefined javascript #promise undefined node js #promise is undefined javascript #undefined is not a promise javascript #javascript promise is undefined ie 11 #ie javascript promise is undefined #promise is undefined ie11 javascript #javascript promise resolver undefined is not a function #javascript promise value undefined #javascript promise return undefined #javascript promise resolve undefined #javascript promise response undefined #async await promise in node js #difference between promise and async await in node js #async await and promises in node js #async await promise in nodejs #async promise node js #async function with promise #callback promise async await nodejs #promise await nodejs #what is difference between promise and async await in node js #async await promise node js #promises and async await in javascript #async await with promise example #async await with promise node js #async promise nodejs #resolve promise in node js #resolve and reject in promise node js #promise-settle node js #resolve a promise node.js #promise.resolve in node js #resolve promise in javascript #promise resolve reject nodejs #promise object in node js #how to resolve promise in node js #how to resolve a promise in node js #q.defer nodejs #return new promise in node js #return promise in node js #resolve() in node js

, , , , ,

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site