Created
March 23, 2018 01:37
-
-
Save gmantri/e68ac10890df58ef46ed51eec34b45a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const Promise = require('bluebird'); | |
const createEmptyFileOfSize = (fileName, size) => { | |
return new Promise((resolve, reject) => { | |
try { | |
fd = fs.openSync(fileName, 'w'); | |
fs.writeSync(fd, Buffer.alloc(1), 0, 1, Math.max(0, size-1)); | |
fs.closeSync(fd); | |
resolve(true); | |
} catch (error) { | |
reject(error); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since you create a Promise, the executor function passed to the
Promise
constructor should end as soon as possible (it is called by thePromise
constructor).The
Promise
constructor should complete immediately, leaving the Promise in thepending
state. The Promise will complete its assignment asynchronously and change its status by calling the providedresolve
orreject
callbacks.Use
setTimeout(..., 0)
to postpone the execution of thetry/catch
block after the completion of the current JavaScript event loop.