如何使用 Q 在 promise 中包装同步函数
How to wrap synchronous function in promise using Q
我正在使用 mocha 对几个函数编写集成测试,其中一些是同步的,另一些是异步的(returns Q promise)。
我有三个函数,A
、B
和 C
。每个函数 returns 下一个函数要使用的值。
A
和 C
是同步的,但是 B
returns 是一个承诺。我需要按 A
> B
> C
.
的顺序调用它们
如果它们都是同步的,它将看起来像这样:C(B(A(args)))
.
现在,我的代码如下所示:
it('should pass this test', function () {
return B(A('args'))
.then(result => Q(C(result)))
.then(result => {
// bunch of assert statements
})
})
我不喜欢这行:.then(result => Q(C(result)))
在bluebird
中我会做.then(Promise.method(C))
,但我必须在这种情况下使用Q,而我在Q中找不到类似的方法。
有没有办法在 Q 中做到这一点?
你不需要换行C
return B(A('args'))
.then(C)
.then(resultOfC => ...
我正在使用 mocha 对几个函数编写集成测试,其中一些是同步的,另一些是异步的(returns Q promise)。
我有三个函数,A
、B
和 C
。每个函数 returns 下一个函数要使用的值。
A
和 C
是同步的,但是 B
returns 是一个承诺。我需要按 A
> B
> C
.
的顺序调用它们
如果它们都是同步的,它将看起来像这样:C(B(A(args)))
.
现在,我的代码如下所示:
it('should pass this test', function () {
return B(A('args'))
.then(result => Q(C(result)))
.then(result => {
// bunch of assert statements
})
})
我不喜欢这行:.then(result => Q(C(result)))
在bluebird
中我会做.then(Promise.method(C))
,但我必须在这种情况下使用Q,而我在Q中找不到类似的方法。
有没有办法在 Q 中做到这一点?
你不需要换行C
return B(A('args'))
.then(C)
.then(resultOfC => ...