[英]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. 我正在使用
@types
定义并添加@types/bluebird
,正如预期的那样,当使用库中的方法时,我会获得自动完成。 However I would like to promisify node's functions (fs). 但是,我想宣传节点的功能(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. 问题是,当我做fsPromisified时,我失去了自动完成功能。
Is there any way I can do this without having to wrap around node's functions myself with promises? 有没有什么方法可以做到这一点,而不必自己用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). 你可以使用pre-promisified
mz /fs
而不是fs
,它包含所有promisifies所有async fs
函数(使用它们的原始名称,而不是Async
前缀)。 That package has TypeScript typings ( @types/mz
): 该软件包具有TypeScript
@types/mz
( @types/mz
):
import * as fs from 'mz/fs';
// e.g., read file
fs.readFile('somefile')
.then((contents) => {
// ...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.