简体   繁体   中英

Is there any way to promisify node's functions and get intellisense with Typescript?

I'm using @types definitions and added @types/bluebird , as expected I get autocomplete when using the methods from the library. However I would like to promisify node's functions (fs). I'm able to do that with the following line:

import * as Promise from 'bluebird'
import * as fs from 'fs'

const fsPromisified = Promise.promisifyAll(fs)

The problem is that when I do fsPromisified I lose autocomplete.

Is there any way I can do this without having to wrap around node's functions myself with promises?

You can use the pre-promisified mz /fs instead of fs , which wraps all promisifies all async fs functions (with their original names, not the Async prefix). That package has TypeScript typings ( @types/mz ):

import * as fs from 'mz/fs';

// e.g., read file
fs.readFile('somefile')
  .then((contents) => {
    // ...
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM