0

i have some callbacks inside my promise:

var res = new Promise(resolve => {
  console.log('trig1');
  var out = fs.createWriteStream(pathToFile);
  console.log('trig2');
  out.on('finish', function() {
    console.log('out finish')
  })
  out.on('close', function() {
    console.log('out close')
  })
  out.on('error', function(err) {
    console.log('out error: ' + err)
  })
})

When i call this promise, it creates the file on the path and prints:

trig1
trig2

Nothing more. Why are my callbacks not executed?

Greetings and thanks

1 Answer 1

1

Because there was no error, you didn't write anything, and you didn't close this stream. That's why any of those events fired.

Try to change your code so it will do something.

var res = new Promise(resolve => {
  console.log('trig1');
  var out = fs.createWriteStream(pathToFile);
  console.log('trig2');
  out.on('finish', function() {
    console.log('out finish')
  })
  out.on('close', function() {
    console.log('out close')
  })
  out.on('error', function(err) {
    console.log('out error: ' + err)
  });
  out.close(); // this will fire `close` event
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.