Advertisement
pharmokan

promisify basic non-async operations javascript

May 19th, 2020
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. var existsAsync = function(path) {
  2. return new Promise(function(resolve, reject) {
  3. fs.exists(path, function(exists) {
  4. // exists is a boolean
  5. if (exists) {
  6. // Resolve successfully
  7. resolve();
  8. } else {
  9. // Reject with error
  10. reject(new Error('path does not exist'));
  11. }
  12. });
  13. });
  14. // Use as a promise now
  15. existsAsync('/path/to/some/file').then(function() {
  16. console.log('file exists!');
  17. }).catch(function(err) {
  18. // file does not exist
  19. console.error(err);
  20. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement