将参数传递给生成器函数
Pass arguments to a generator function
过去几个小时我一直被困在这个问题上。
如何将参数传递给生成器函数?
function* getFoo(foo) {
return yield Promise.resolve(foo + 10);
}
exports.testRoute = Promise.coroutine(function* (req, res) {
let bar = yield Promise.coroutine(getFoo); // <--- how to pass argument??
res.send(bar.toString());
});
Current Code throws Error(我知道它指向我 here 但它没有说明参数传递):
Unhandled rejection TypeError: A value [object Promise] was yielded that could not be treated as a promise
See http:// goo.gl/4Y4pDk
From coroutine:
at Function.module.exports.Promise.coroutine (d:\Workspace\Github\API-NodeJS\app\node_modules\bluebird\js\main\generators.js:111:17)
如果我这样做:
let bar = yield Promise.coroutine(getFoo(5));
我得到以下错误(再次自我解释错误,但 this link 也没有解释参数传递):
Unhandled rejection TypeError: generatorFunction must be a function
See http:// goo.gl/6Vqhm0
at Function.module.exports.Promise.coroutine (d:\Workspace\Github\API-NodeJS\app\node_modules\bluebird\js\main\generators.js:107:15)
我相信你想要的是这个:
function* getFoo(foo) {
return yield Promise.resolve(foo + 10);
}
exports.testRoute = Promise.coroutine(function* (req, res) {
let bar = yield Promise.coroutine(getFoo)(50);
res.send(bar.toString());
});
您需要了解 Promise.coroutine
的作用。它需要一个生成器,它 return 是一个函数,return 是一个承诺。
正如您在第一种情况 (yield Promise.coroutine(getFoo);
) 中看到的,您正在生成 Promise.coroutine
的结果,它是一个函数而不是一个 promise,这会导致错误:
A value [object Promise] was yielded that could not be treated as a promise
在第二种情况下 (yield Promise.coroutine(getFoo(5));
),您只是在启动生成器。 getFoo(5)
return 是一个 "generator" 对象,但是 Promise.coroutine
想要一个 "generator function",结果是:generatorFunction must be a function
.
事实上 bluebird 将 Promise.coroutine 的结果显示为 [object Promise]
造成了很大的混乱,因为 Promise.coroutine 应该 return 一个函数,但我不能弄清楚。我想你可以把它作为另一个问题来问。
过去几个小时我一直被困在这个问题上。
如何将参数传递给生成器函数?
function* getFoo(foo) {
return yield Promise.resolve(foo + 10);
}
exports.testRoute = Promise.coroutine(function* (req, res) {
let bar = yield Promise.coroutine(getFoo); // <--- how to pass argument??
res.send(bar.toString());
});
Current Code throws Error(我知道它指向我 here 但它没有说明参数传递):
Unhandled rejection TypeError: A value [object Promise] was yielded that could not be treated as a promise
See http:// goo.gl/4Y4pDk
From coroutine:
at Function.module.exports.Promise.coroutine (d:\Workspace\Github\API-NodeJS\app\node_modules\bluebird\js\main\generators.js:111:17)
如果我这样做:
let bar = yield Promise.coroutine(getFoo(5));
我得到以下错误(再次自我解释错误,但 this link 也没有解释参数传递):
Unhandled rejection TypeError: generatorFunction must be a function
See http:// goo.gl/6Vqhm0
at Function.module.exports.Promise.coroutine (d:\Workspace\Github\API-NodeJS\app\node_modules\bluebird\js\main\generators.js:107:15)
我相信你想要的是这个:
function* getFoo(foo) {
return yield Promise.resolve(foo + 10);
}
exports.testRoute = Promise.coroutine(function* (req, res) {
let bar = yield Promise.coroutine(getFoo)(50);
res.send(bar.toString());
});
您需要了解 Promise.coroutine
的作用。它需要一个生成器,它 return 是一个函数,return 是一个承诺。
正如您在第一种情况 (yield Promise.coroutine(getFoo);
) 中看到的,您正在生成 Promise.coroutine
的结果,它是一个函数而不是一个 promise,这会导致错误:
A value [object Promise] was yielded that could not be treated as a promise
在第二种情况下 (yield Promise.coroutine(getFoo(5));
),您只是在启动生成器。 getFoo(5)
return 是一个 "generator" 对象,但是 Promise.coroutine
想要一个 "generator function",结果是:generatorFunction must be a function
.
事实上 bluebird 将 Promise.coroutine 的结果显示为 [object Promise]
造成了很大的混乱,因为 Promise.coroutine 应该 return 一个函数,但我不能弄清楚。我想你可以把它作为另一个问题来问。