Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const exec = require('child_process').exec,
- D = console.log;
- function runShellCommand(command) {
- return new Promise((resolve,reject) => {
- let outmessage,
- errormessage
- let runner = exec(command, function (err, stdout, stderr) {
- if (err) {
- //D(`stderr : ${stderr}`)
- //D(`out : ${stdout}`)
- //D(`err : ${err}`)
- reject(stderr)
- }
- outmessage=stdout
- errormessage=stderr
- })
- runner.on('close', function(exitcode) {
- resolve({outmessage,exitcode})
- })
- })
- }
- function chainPromises(){
- runShellCommand("df -h")
- .then(res1 => runShellCommand(`echo "${res1.outmessage}" > command1.log`))
- .then(res2 => runShellCommand(`echo exit code "${res2.exitcode}" > command2.log`))
- }
- function unsyncResult(){
- let res1 = runShellCommand("df -h")
- let res2 = runShellCommand(`echo "${res1.outmessage}" > command1.log`)
- let res3 = runShellCommand(`echo exit code "${res2.exitcode}" > command2.log`)
- }
- async function asyncMode(){
- let res1 = await runShellCommand("df -h")
- let res2 = await runShellCommand(`echo "${res1.outmessage}" > command1.log`)
- let res3 = await runShellCommand(`echo exit code "${res2.exitcode}" > command2.log`)
- }
- async function asyncCatch(){
- runShellCommand("df -h")
- .then(res1 => runShellCommand(`echio "${res1.outmessage}" > command1.log`))
- .catch(e => {throw e})
- .then(res2 => runShellCommand(`echioo exit code "${res2.exitcode}" > command2.log`))
- //.catch(e => console.log(e))
- }
- function promiseList(){
- var promise1 = Promise.resolve(3);
- var promise2 = 42;
- var promise3 = new Promise(function(resolve, reject) {
- setTimeout(resolve, 3000, 'foo');
- });
- Promise.all([promise3, promise2, promise1]).then(function(values) {
- console.log(values);
- //HAGO mis cosas
- });
- }
- //unsyncResult()
- //console.log(asyncMode())
- //chainPromises()
- //asyncCatch()
- console.log(promiseList())
- console.log("ddd")
- //calbacks()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement