close

How to return data from promise

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to return data from promise in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to return data from promise ?

  1. How to return data from promise ?

    to return data from promise You have to return a promise instead of a variable. So in your function just return:
    return relationsManagerResource.GetParentId(nodeId)
    And later resolve the returned promise. Or you can make another deferred and resolve theParentId with it.

  2. How to return data from promise ?

    to return data from promise You have to return a promise instead of a variable. So in your function just return:
    return relationsManagerResource.GetParentId(nodeId)
    And later resolve the returned promise. Or you can make another deferred and resolve theParentId with it.

Method 1

One of the fundamental principles behind a promise is that it’s handled asynchronously. This means that you cannot create a promise and then immediately use its result synchronously in your code (e.g. it’s not possible to return the result of a promise from within the function that initiated the promise).

What you likely want to do instead is to return the entire promise itself. Then whatever function needs its result can call .then() on the promise, and the result will be there when the promise has been resolved.

Here is a resource from HTML5Rocks that goes over the lifecycle of a promise, and how its output is resolved asynchronously:

Method 2

You have to return a promise instead of a variable. So in your function just return:

return relationsManagerResource.GetParentId(nodeId)

And later resolve the returned promise. Or you can make another deferred and resolve theParentId with it.

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment