1

I want to implement waterfall with async and await.

i went through this tried some hands on

The main purpose is i do want to want call async function if any one of above async rejects with an error.

var cont = 1;

function signin (){
  console.log(cont);
  // i am doing some async
  return new Promise((a,b)=>{
    if((++cont) === 4){
      setTimeout(1000*cont,b,'ab');      
    } else {
      setTimeout(1000*cont,a,'ab');
    }
  })
}

async function main() {
  try {
    await signin;
    await signin;
    await signin;
    await signin;
    await signin;
    await signin;
    await signin;
    return signin();
  } catch(er){
    return er;
  }
}

main()

In the above, nothing works as expected. what my expected output output is

1
2
3
4

After fourth it should break all the way. And rest of the attempts to signin should not be called.

All signin must be executed in series. Like in waterfall.

Any help?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
codeofnode
  • 18,169
  • 29
  • 85
  • 142
  • Shouldn't it be `await signin();` in your main function you just have `await signin;`. – Frank Roth May 29 '17 at 10:16
  • why https://stackoverflow.com/a/40593103/1503495 does not `call` those functions? Sorry if that is silly, but i have just started, async and await. – codeofnode May 29 '17 at 10:21
  • even with `await signin();` it does not go after 1. – codeofnode May 29 '17 at 10:23
  • I don't know but your code does not make any sense at all. The `setTimeout(1000*cont,b,'ab');` is also wrong. First param ist the function and the second param is the time in ms. Please read: https://www.w3schools.com/jsref/met_win_settimeout.asp – Frank Roth May 29 '17 at 10:24
  • FYI, `async/await` will be part of ES2017. It is *not* part of ES7 (ES2016). – Felix Kling May 29 '17 at 15:14

1 Answers1

0

2 things you need to change:

  • the parameters of the setTimeout function
  • invoke the signin function with paranthesis like: await signin()

      var cont = 1;
      function signin (){
      console.log(cont);
      // i am doing some async
      return new Promise((resolve, reject)=>{
        if((++cont) === 4){
          setTimeout(reject(cont),1000*cont);
        } else {
          setTimeout(resolve(cont),1000*cont);
        }
      })
    }
    
    async function main() {
      try {
        await signin();
        await signin();
        await signin();
        await signin();
        await signin();
        await signin();
        await signin();
        return signin();
      } catch(er){
        console.log("error: " +cont);
        return er;
      }
    }
    
    main()
    
CBri
  • 101
  • 3
  • one more question, what if i want to pass the parameter to signin.. Say the when first signin is resolved with 'hello world', second signin should read that as parameter and prepend 'second' so that thir signin should recieve that as 'second hello world'.. and so on.., can you give me a sandbox.. i am currently doing on babel try,, but there i am not able to find the sharing link – codeofnode May 29 '17 at 12:18
  • @codeofnode: `async/await` lets you write code in a synchronous fashion (to some degree): `var result = await signin(); result = await signin('second ' + result);` – Felix Kling May 29 '17 at 15:17