Skip to content

Instantly share code, notes, and snippets.

@t-okushima
Last active January 19, 2019 12:26
Show Gist options
  • Save t-okushima/e35726cf1b428df656a4d3e403624f5f to your computer and use it in GitHub Desktop.
Save t-okushima/e35726cf1b428df656a4d3e403624f5f to your computer and use it in GitHub Desktop.
util.promisify()でcallbackスタイルの関数をpromise化 ref: https://qiita.com/t-okushima/items/fbca67541003576997cc
'use strct';
const fs = require('fs');
const util = require('util');
const readFileAsync = util.promisify(fs.readFile);
const filePath = './test.txt';
async function promisifyTest() {
try {
const text = await readFileAsync(filePath, {encoding : 'utf8'});
console.log(text);
} catch (error) {
console.log(error);
}
}
promisifyTest();
'use strct';
const fs = require('fs');
const util = require('util');
const readFileAsync = util.promisify(fs.readFile);
const filePath = './test.txt';
async function promisifyTest() {
try {
const text = await readFileAsync(filePath, {encoding : 'utf8'});
console.log(text);
} catch (error) {
console.log(error);
}
}
promisifyTest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment