Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise inside promise: what's the correct way to return a variable from the child promise? (JS)

I've a function like this:

function top() {    //promise1   ParentPromise({     ...some code here...   }).then(function() {       //promise2         ChildPromise({           ..some code here...         }).then(function(response) {          var result = response.result.items;          });  });  }; 

and i need to return result value in this way:

var myresult = start(); 

How i can do that? THX

like image 870
Valerio Marzulli Avatar asked May 10 '17 14:05

Valerio Marzulli


People also ask

How do I return a promise to a promise?

Here we need to first declare a Promise by using the Promise syntax, and we will be using the then() method for its execution and then inside that then() method we will create another promise by using the same Promise syntax as illustrated above, and then we will call our result of first inside that new Promise.

What does promise return in JavaScript?

The Promise object supports two properties: state and result. While a Promise object is "pending" (working), the result is undefined. When a Promise object is "fulfilled", the result is a value. When a Promise object is "rejected", the result is an error object.

How do you assign a value of a promise to a variable?

To assign value from successful promise resolve to external variable with JavaScript, we use async and await. const myFunction = async () => { vm. feed = await getFeed(); // ... }; to get the resolve value of the promise returned by getFeed with await .


2 Answers

The definition of promises is that you cannot literally assign result to myresult. However, you can make myresult a promise that resolves directly to result for the caller, however many promises were used to get that. The basic idea is that inside of each function in your above block, you should be returning the next Promise in the chain. eg:

function top() {    //promise1   return ParentPromise({     ...some code here...   }).then(function() {       //promise2         return ChildPromise({           ..some code here...         }).then(function(response) {          var result = response.result.items;          return result;          });  });  }; 

In the end, the code calling top() won't know or care that 1, 2, or 12 chained promises were used to get result. It will also be able to register an error callback in case any of those promises failed.

like image 67
Katana314 Avatar answered Sep 28 '22 00:09

Katana314


The neatest way in my opinion is to return the promise and chain them 'down' instead of to the left, avoiding christmas tree-like callback hell.

function top() {   //promise1   return ParentPromise({     ...some code here...   }).then(function(parent) {     //promise2     return ChildPromise(parent.id)   }).then(function(response) {     // do something with `result`      return response.result.items;   }); }  top().then(function(items){   // all done  }); 

Edit: Or in ES6 / lambda notation;

function top() {   return ParentPromise().then(parent => {     return ChildPromise(parent.id)   }).then(response => {     return response.result.items   }) }  top().then(items => {   // all done  }) 

Edit: Or using Async/Await;

async function top() {   const parent = await ParentPromise()   const child = await ChildPromise(parent.id)    return child.result.items }  top().then(items => {   // all done  }) 
like image 44
Erik Terwan Avatar answered Sep 27 '22 23:09

Erik Terwan