我正在尝试promisify标准节点fs. writeFile。我有node和bluebird的类型,这是我在这里选择的Promise库:
const f = Promise.promisify(fs.writeFile)
return f(file, content); // Should be a promise
这不起作用,因为:
[ts]提供的参数与调用目标的任何签名都不匹配。const f:(arg1:string)=> Promise<{}>
所以我选择了错误的重载方法,因为对promisify的调用不能真正知道我猜的是什么。或者甚至不是这样,而是带有可选参数的东西,因为这是三个重载的writeFile:
export function writeFile(filename: string, data: any, callback?: (err: NodeJS.ErrnoException) => void): void;
export function writeFile(filename: string, data: any, options: { encoding?: string; mode?: number; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;
export function writeFile(filename: string, data: any, options: { encoding?: string; mode?: string; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;
promisify有很多# of args的重载,看起来像这样:
/**
* Returns a function that will wrap the given `nodeFunction`. Instead of taking a callback, the returned function will return a promise whose fate is decided by the callback behavior of the given node function. The node function should conform to node.js convention of accepting a callback as last argument and calling that callback with error as the first argument and success value on the second argument.
*
* If the `nodeFunction` calls its callback with multiple success values, the fulfillment value will be an array of them.
*
* If you pass a `receiver`, the `nodeFunction` will be called as a method on the `receiver`.
*/
static promisify<T>(func: (callback: (err:any, result: T) => void) => void, receiver?: any): () => Promise<T>;
static promisify<T, A1>(func: (arg1: A1, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1) => Promise<T>;
static promisify<T, A1, A2>(func: (arg1: A1, arg2: A2, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2) => Promise<T>;
static promisify<T, A1, A2, A3>(func: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3) => Promise<T>;
static promisify<T, A1, A2, A3, A4>(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Promise<T>;
static promisify<T, A1, A2, A3, A4, A5>(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Promise<T>;
static promisify(nodeFunction: Function, receiver?: any): Function;
因此,它似乎是错误的承诺,而正在使用。有没有更干净的方法让我达到我想要的?目前我必须解决这个问题:
const writeFile : (filename: string, data: any, callback: (err: NodeJS.ErrnoException) => void) => void = fs.writeFile
const f = Promise.promisify(writeFile)
return f(file, content);
我觉得这很难看也很冗长
3条答案
按热度按时间yh2wf1be1#
我还没有能够建立一个好的简单的工作示例,但下面的类型Assert将允许您提示和您所期望的:
您可以修改
<(file: string, content: string) => void>
以更好地描述您期望可用的内容,或者使用更广泛的类型<Function>
或使用<any>
进行动态处理。第一个选项需要做更多的工作,但它允许在以后调用
f
时推断出返回类型(在本例中为void
-但可以想象在某些情况下会更频繁地使用此返回类型)。根据你的评论我猜你需要...
在这个例子中,如果你知道两种可能的结果,你也可以使用一个联合类型来代替
any
。Promise<any>
可以是:这将允许返回类型为
Promise<ErrnoException>
或Promise<MySuccessfulType>
。8cdiaqws2#
你可以从
fs.writeFile
命名空间使用__promisify__
:否则,你必须显式地提到泛型类型来选择相应的重载:
util
包,具有promisify()
功能mccptt673#
我最终做的是指定自己的重载类型,这个方法的主要缺点是,一旦库更新,我不会得到通知。
mixpanel的例子: