Last active
May 29, 2019 16:56
-
-
Save vbarbarosh/a1a44a02b98191aa9e88652214293444 to your computer and use it in GitHub Desktop.
node_promise_promisify – Ways to promisify a function https://codescreens.com
This file contains hidden or 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 Promise = require('bluebird'); | |
| const fs = require('fs'); | |
| const util = require('util'); | |
| // Ways to promisify a function | |
| // $ node node_promise_promisify.js | |
| // https://nodejs.org/api/util.html#util_util_promisify_original | |
| const fs_open = util.promisify(fs.open); | |
| // http://bluebirdjs.com/docs/api/promise.promisify.html | |
| const fs_unlink = Promise.promisify(fs.unlink); | |
| // http://bluebirdjs.com/docs/api/promise.promisifyall.html | |
| Promise.promisifyAll(fs); | |
| Promise.promisifyAll(fs, {suffix: '_async'}); | |
| main().catch(panic); | |
| async function main() | |
| { | |
| // Promise.method: function will always return promise | |
| Promise.resolve(1) | |
| .then(Promise.method(function () { console.log('foo'); })) | |
| .then(Promise.method(async function () { console.log('bar'); })); | |
| console.log('end'); | |
| } | |
| async function panic(error) | |
| { | |
| console.error(error); | |
| process.exit(1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment