0

I've the following promise which work well

troces.run(value, "../logs/env.txt")
    .then(function (data) {
        console.log(data);
        return updadeUser(val, arg, args[1])
        // Now here I need to add new method updateAddress(host,port,addr)

    }).catch(function (err) {
        console.error(err);
    });

Now I need to add additional method call inside the the first .then that the update user and the updateAddress will work together

My question are

  1. assume that updateUser will need to start 10 ms after the update address How is it recommended to do so?

  2. in aspects of error handling if one of the process failed (send error message ) I Need to exit (process.exit(1);)

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88

1 Answers1

1

Use .all:

troces.run(value, "../logs/env.txt")
.then(data => {
    console.log(data);
    return Promise.all([updadeUser(val, arg, args[1]),
                        updateAddress(host,port,addr)]);
}); // no need to add catches bluebird will log errors automatically

If you really need the 10ms delay, you can do:

troces.run(value, "../logs/env.txt")
.then(data => {
    console.log(data);
    return Promise.all([updadeUser(val, arg, args[1]),
                        Promise.delay(10).then(x => updateAddress(host,port,addr))]);
}); // no need to add catches bluebird will log errors automatically

Although I suspect that you really just want updateUser to happen before updateAddress which can be easily solved with:

troces.run(value, "../logs/env.txt")
.then(data => {
    console.log(data);
    return updadeUser(val, arg, args[1]).then(_ => updateAddress(host,port,addr));
}); // no need to add catches bluebird will log errors automatically

If you need to exit on promise error, you can do:

process.on("unhandledRejection", () => process.exit(1)); 

Although I warmly recommend you create meaningful error messages, just a non-zero process exit code is hard to debug.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Thanks Benjamin, in this case its better then promise.all? – 07_05_GuyT Jan 21 '16 at 09:48
  • @shopiaT no, it is better to use `.all` than `join` in this case, I changed my answer already :) – Benjamin Gruenbaum Jan 21 '16 at 09:49
  • Thank you 1+! you are fast :), the problem which I cannot use the last suggestion is I dont know when the update user will be resolved... – 07_05_GuyT Jan 21 '16 at 09:51
  • @shopiaT also, I would appreciate it if you told me what wasn't clear in the docs, I've slightly modified them right now. We're looking to improve the docs so - suggestions appreciated. As for "I don't know when the update user will be resolved" - that's a big problem and you should fix that first. `updateUser` already returns a promise - have that promise resolve when the update finishes. – Benjamin Gruenbaum Jan 21 '16 at 09:56
  • HI Benjamin, which docs you refer too? the are many ... bluebird?(i like this lib :) can you please provide a link. about the resolve it can take some time so I dont know actually when it will be resolved(in some cases it is spawn some process...,why this is a problem? – 07_05_GuyT Jan 21 '16 at 10:05
  • Docs are at http://bluebirdjs.com - if you didn't see them then there isn't much I can do about that though. You can still convert APIs like `exec` to use promises and listen for when the process ends. – Benjamin Gruenbaum Jan 21 '16 at 10:17
  • Any reason not to flatten the `then`s in the third snippet? – Bergi Jan 21 '16 at 10:22
  • @Bergi just so it looks visually similar to the other two. Nothing too exciting. – Benjamin Gruenbaum Jan 21 '16 at 11:20
  • @Bergi - what do you mean flatting? – 07_05_GuyT Jan 21 '16 at 11:49
  • @shopiaT: [flattening the nested callbacks](http://stackoverflow.com/a/22000931/1048572) – Bergi Jan 21 '16 at 16:19