Promise Node js

Asked

Viewed 44 times

0

I make 3 HTTPS nodejs requests to an API also in Node, each Promise performs a task and these Promises I am passing several information on their body, one of these information needs to be an ID. Running a Promise all, with the 3 Promises, the Promise example_2 returns me an ID and I want to pass this ID to the Promise example_3, returning the result I need. So the result of Promise all has to be from Promise example_3. And I have no idea how to do that!

let example_3 = () =>{
   return new Promise( ( resolve, reject ) =>{
     try{
        request({
            method:'post',
            body:'AQUI PRECISO DO ID DO EXAMPLE_2'
       },error(.....


//Executo todas promises
let exect = async () => { 
   let responses    = await Promise.all([example_1,example_2,example_3])
   let responseJson = JSON.parse(responses[1]);
   //o id da promise example_2
   let idExample2   = JSON.parse(responseJson.data.filter_value);
}

The id I want to pass to the file EXAMPLE_2 is in the variable idResp. Could someone guide me?

  • tries to improve the body of your question, which technology you use, is a bank? a JSON document? This is not clear leave all possible information. which type of bank is a bank and what exactly has in this bank model, all this has to be passed.

  • thanks @Teuuz1994, I will improve the question

1 answer

1


Since you’re using async / await you can’t do it that way ?

let example_3 = (id) =>{
   return new Promise( ( resolve, reject ) =>{
     try{
        request({
            method:'post',
            body: id
       },error(.....


//Executo todas promises
let exect = async () => { 
   let promise1 = await example_1()
   let promise2 = await example_2()

   let promise3 = await example_3(promise2.data.filter_value)
}

You can also parallelize calls to improve performance.

let exect = async () => { 
       let promise2 = await example_2()

       let promise1 = example_1()
       let promise3 = example_3(promise2.data.filter_value)

       let responsePromise1 = await promise1
       let responsePromise3 = await promise3

    }
  • Thanks, with your example gave me a light, I declared a global variable and threw the id inside it. was thus: state.myId = JSON.parse(responseJson.data.filter_value); Let examp = await example_3();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.