Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have a few errors in your examples.</p> <h3>read1</h3> <p>This is not 'make a promise manually', this is just making a normal asynchronous call. In your code, you call <code>readFile</code> immediately, so <code>read1</code> would be the return value of <code>readFile</code> which is <code>undefined</code>. To get a behavior similar to <code>read2</code> and <code>read3</code> you would need to do something like this:</p> <pre><code>var read1 = function(fname, env, success, error){ fs.readFile(fname, enc, function (err, data) { // Throwing here would just crash your application. if(err) error(err); // Returning from inside 'readFile' does nothing, instead you use a callback. else success(data); }); }; </code></pre> <h3>read2</h3> <pre><code>// Not equivalent to read1 because of the notes above, // Equivalent to read3, with the fixes I mention below. var read2 = Q.nfbind(fs.readFile); </code></pre> <h3>read3</h3> <pre><code>var read3 = function (fname, enc) { var deferred = Q.defer(); fs.readFile(fname, enc, function (error, text) { if (error) { // 'error' is already an error object, you don't need 'new Error()'. deferred.reject(error); } else { deferred.resolve(text); } // HERE: Again returning a value from 'readFile' does not make sense. return deferred.promise; }); // INSTEAD: Return here, so you can access the promise when you call 'read3'. return deferred.promise. }; </code></pre> <p>You can indeed use <code>nfbind</code> on anything that takes a callback as the last parameter. With my comments, <code>read2</code> and <code>read3</code> accomplish the same goal, which is to create a function that will take a filename and encoding, and return a promise object.</p> <p>For those, you can do this:</p> <pre><code>read2('file.txt', 'utf8').then(function (data) {}, function (err) {}).done(); read3('file.txt', 'utf8').then(function (data) {}, function (err) {}).done(); </code></pre> <p>For <code>read1</code>, you would call it like this:</p> <pre><code>read1('file.txt', 'utf8', function (data) {}, function (err) {}); </code></pre> <h3>Update</h3> <p>Standard promises have evolved a bit since this was answered, and if you are leaning toward <code>read3</code>, I'd recommend doing the following:</p> <pre><code>var read4 = function (fname, enc) { return Q.promise(function(resolve, reject){ fs.readFile(fname, enc, function (error, text) { if (error) { // 'error' is already an error object, you don't need 'new Error()'. reject(error); } else { resolve(text); } }); }); }; </code></pre> <p>This is more in line with standard ES6 promises, and with bluebird, so you'll have an easier time with the code moving forward. Using the method mentioned in <code>read3</code> also introduces the possibility of synchronously throwing exceptions instead of capturing them in the promise chain, which is usually undesirable. See <a href="https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern" rel="nofollow">the deferred antipattern</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload