【发布时间】:2019-01-23 19:17:13
【问题描述】:
考虑以下承诺:
import { S3 } from 'aws-sdk'
import { promisify } from 'util'
const s3 = new S3({ apiVersion: '2006-03-01' })
const getObject = promisify<S3.GetObjectRequest, S3.GetObjectOutput>(
s3.getObject
)
这很好用,除了你会得到像TypeError: this.makeRequest is not a function 这样的错误,因为s3.getObject 现在绑定到错误的this 范围。但是,这个:
const getObject = promisify<S3.GetObjectRequest, S3.GetObjectOutput>(
s3.getObject.bind(s3)
)
破坏类型安全并会报错:Unsafe use of expression of type 'any'.(假设您使用严格模式)。
那么我怎样才能在 Typescript 中承诺像 s3 的 getObject 这样的东西呢?
【问题讨论】:
-
() => s3.getObject()
标签: javascript node.js typescript amazon-s3