Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback" rel="nofollow" title="fs.open&#40;&#41;">fs.open()</a> using the 'w' param will work to create a file. As you proceed, you may encounter a second issue with your code, along the lines of this:</p> <pre><code>/srv/node/test $ node makekill.js successfully created file successfully created file successfully created file successfully created file successfully created file successfully created file successfully created file successfully created file successfully created file successfully created file successfully closed file /srv/node/test/makekill.js:13 throw err; ^ Error: ENOENT, No such file or directory './hello11.txt' </code></pre> <p>With some additional logging, you'll see that the code you're using doesn't actually trigger creation actions for "hello1.txt" through "hello10.txt" followed by deletion actions for the same. (Or in your sample, deletions, followed by creations, though I changed this while debugging because create/delete made more sense to me.)</p> <p>More to the point, the asynchronous callbacks seem to all use the final "i" value, rather than the value of "i" during the relevant loop.</p> <p>Long story short, I made the concept work by making a separate function which performed the desired create/delete for a given filename, and then call this function from the for loop. This seems to be a more reliable way to ensure that the intended value of "i" is used throughout the action sequence, rather than changing between the initial step and the callback step.</p> <pre><code>var fs = require('fs'); function openUnlink(name) { console.log(name); fs.open(name, 'w', function (err) { console.log(' + ' + name); fs.unlink(name, function (err) { console.log(' - ' + name); }); }); } for (var i=1;i&lt;=10;i++) { openUnlink('hello'+i+'.txt'); } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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