Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. const exec = require('child_process').exec,
  2. D = console.log;
  3.  
  4. function runShellCommand(command) {
  5. return new Promise((resolve,reject) => {
  6. let outmessage,
  7. errormessage
  8.  
  9. let runner = exec(command, function (err, stdout, stderr) {
  10. if (err) {
  11. //D(`stderr : ${stderr}`)
  12. //D(`out : ${stdout}`)
  13. //D(`err : ${err}`)
  14. reject(stderr)
  15. }
  16.  
  17. outmessage=stdout
  18. errormessage=stderr
  19.  
  20. })
  21. runner.on('close', function(exitcode) {
  22. resolve({outmessage,exitcode})
  23. })
  24.  
  25. })
  26. }
  27.  
  28.  
  29. function chainPromises(){
  30.  
  31. runShellCommand("df -h")
  32. .then(res1 => runShellCommand(`echo "${res1.outmessage}" > command1.log`))
  33. .then(res2 => runShellCommand(`echo exit code "${res2.exitcode}" > command2.log`))
  34.  
  35. }
  36.  
  37. function unsyncResult(){
  38.  
  39. let res1 = runShellCommand("df -h")
  40. let res2 = runShellCommand(`echo "${res1.outmessage}" > command1.log`)
  41. let res3 = runShellCommand(`echo exit code "${res2.exitcode}" > command2.log`)
  42.  
  43. }
  44.  
  45.  
  46. async function asyncMode(){
  47. let res1 = await runShellCommand("df -h")
  48. let res2 = await runShellCommand(`echo "${res1.outmessage}" > command1.log`)
  49. let res3 = await runShellCommand(`echo exit code "${res2.exitcode}" > command2.log`)
  50. }
  51.  
  52. async function asyncCatch(){
  53.  
  54. runShellCommand("df -h")
  55. .then(res1 => runShellCommand(`echio "${res1.outmessage}" > command1.log`))
  56. .catch(e => {throw e})
  57. .then(res2 => runShellCommand(`echioo exit code "${res2.exitcode}" > command2.log`))
  58. //.catch(e => console.log(e))
  59.  
  60. }
  61.  
  62.  
  63. function promiseList(){
  64.  
  65. var promise1 = Promise.resolve(3);
  66. var promise2 = 42;
  67. var promise3 = new Promise(function(resolve, reject) {
  68. setTimeout(resolve, 3000, 'foo');
  69. });
  70.  
  71. Promise.all([promise3, promise2, promise1]).then(function(values) {
  72. console.log(values);
  73.  
  74. //HAGO mis cosas
  75. });
  76.  
  77. }
  78.  
  79.  
  80. //unsyncResult()
  81. //console.log(asyncMode())
  82. //chainPromises()
  83. //asyncCatch()
  84. console.log(promiseList())
  85. console.log("ddd")
  86. //calbacks()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement