0

How can I chain the parameters that I need for my both async functions.

The first function fs.readFile returns the content of the file in the callback function as second parameter.

The second function marked requires this content as first parameter. The second parameter is optional and can be an options object. The third parameter is the callback that should give me the converted content as second parameter.

Currently I've tried this code:

  var readFile = q.nfbind(fs.readFile);
  var md = q.nfbind(marked);

  readFile(fileName, 'UTF8')
  .then(md)
  .then(function (html) {
    res.setHeader('Content-Type', 'text/html');
    res.setHeader('Content-Length', html.length);
    res.status(200);
    res.end(html);
  })
  .catch(function (error) {
    res.setHeader('Content-Type', 'text/plain');
    res.send(500, 'server error: ' + error);
    res.end();
  })
  .done();

But it doesn't work, because the marked function needs the second parameter when it was called with an callback function as third parameter. How can I set the second parameter, to call the marked function correctly?

1 Answer 1

1

If you simply replace the .then(md) line with .then(marked), then the result of calling fs.readFile (the value with which the promise was fulfilled) will be passed to marked.

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.